Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

Conceptual question for problem set 9: Why do we need to make a specific __iter__? And why does my for loop print, end in a error of "NoneType"? (could be related)

OpenStudy (anonymous):

Also is it possible to pull out the name of the instance? Like if I did: class Square: def __init__(self, h): self.side = float(h) squ1 = Square(5) After I make the instance, how do I retrieve the name "squ1"? Because when I use: self.name = self, it just shows a pointer to the memory. Thanks for any help!

OpenStudy (anonymous):

FYI, if you get a NoneType error, it may be because your function lacks a return. = P

OpenStudy (anonymous):

not sure what u r asking for: http://codepad.org/gnhOynWk

OpenStudy (anonymous):

So it seems like you have to pass the variable name in as an argument. I guess you can technically automate that. So it's not that bad. I think I see how it all makes sense now. Thanks

OpenStudy (anonymous):

As for the __iter__() question, I just returned self.shapes.__iter__() from ShapeSet.__iter__() because that made more sense. Then I thought that wasn't the point of the problem, so I implemented my own class. It was a good exercise, but as you've alluded to, not what you'd do in the real world. Though, come to think of it, it would be necessary if you didn't use a list or set to store the Shapes. Since you have to print them in groups (by Shape type) it might make sense to store them in groups, so you'd have to write your own iterator to iterate through all the elements across those groups.

OpenStudy (anonymous):

knowing how to and having the experiance of creating an object that is iterable might come in very handy sometime in the future

OpenStudy (anonymous):

yeah i can see why programming this concept is important, but for the problem it doesn't seem to do much. This incidentally makes it hard to see the value of overriding the iter. In terms of actually trying to implement it, I basically added a next function and copied the code from the lecture handout (regarding mit and faculty etc). Did you guys do the same? Thanks!

OpenStudy (anonymous):

This is the approach I use in Java. It's probably not as applicable to python because python iterators only have the next() function, while Java use hasNext()/next(). The base class (AbstractIterator) implements the contract of the next() function (return the item when asked, raise an exception when out of items), and delegates the task of finding items to subclasses via the findNext() method. The subclass (SequenceIterator in this example) is only concerned with finding the next item. Java iterators use two methods, hasNext() and next(), and they both need to know if there are more items left to be iterated, so it makes more sense to have one method whose only responsibility it is to find items. But I usually use Java, so it makes more sense for me to think about iterators this way.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!