If a class defines an .__iter__()
method, that method is called:
Whenever the built-in iter()
function is applied to an instance of the class.
In any situation where the instance is iterated
over, such as when it appears as the controlling
sequence of a for statement.
The calling sequence is:
def __iter__(self):
...
The return value must be an iterator. An iterator is
any object that has a .next() method
that returns the next value in sequence, or raises
StopIteration when the sequence is
exhausted. For more information, see Section 17.2, “Iterators: Values that can produce a sequence of
values”.