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

Hi all. I've been doing pset 9 and have read the documentation on __iter__ but still don't understand how it works. Instead of being an iterator, I'm just using this as a function, which I think is incorrect. Can somebody explain __iter__ and, if it's related, next() [I didn't understand how Prof Grimson used towards the end of lec 16]? This is the first, defining __iter__. But unlike what it says in the triple quotes, I return a whole list instead of a single shape: http://dpaste.com/589943/ The second, findLargest, is how I've used __iter__: http://dpaste.com/589942/ Thanks

OpenStudy (anonymous):

Ok, so basically __iter__ is a method that must return an iterator for your object. What is being iterated is entirely up to you. An iterator is just an object that has a next() method which returns different items as it is iterated. It may also raise certain iterator exceptions. The problem with your __iter__ method is that you're actually doing the iteration there.. Also do not use __iter__ directly. If you want an iterator, use the iter builtin function: e.g.: iter(shapes) NOT: shapes.__iter__()

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!