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

Hi fellow OCW students, I'm kind of lost on problem 2 of Problem set 9. I still don't get the idea of __iter__(self): Can you guys give me some explanation on what it is and what it does?

OpenStudy (anonymous):

you are supposed to return an object that will 'yield' the next item when its next method is called. http://docs.python.org/2.7/reference/datamodel.html#special-method-names http://docs.python.org/2.7/reference/datamodel.html#object.__iter__ http://docs.python.org/2.7/library/stdtypes.html#iterator-types http://docs.python.org/2.7/tutorial/classes.html#iterators A google search of - python custom class iterator - returns a number of useful 'hits'

OpenStudy (anonymous):

A simple, but clear example from http://johnmc.co/llum/the-easiest-way-to-implement-__iter__-for-a-python-object/ Let's say you write your own class (and it stores a list of vertices and you want to use loop over all vertices using a for loop. How does the for loop know what the next value of your object would be? That's where the __iter__ method comes in. In the __iter__ method, you'll need to say what data is returned when you want to iterate over an object. They can be strings, numbers, anything you want.

OpenStudy (anonymous):

Thank you guys!

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!