Despite reading up on it, I still dont quite understand how __iter__
works. What would be a simple explaination?
I\'ve seen def__iter__(self): retu
An iterator needs to define two methods: __iter__()
and __next__()
(next()
in python2). Usually, the object itself defines the __next__()
or next()
method, so it just returns itself as the iterator. This creates an iterable that is also itself an iterator. These methods are used by for
and in
statements.
Python 3 docs: docs.python.org/3/library/stdtypes.html#iterator-types
Python 2 docs: docs.python.org/2/library/stdtypes.html#iterator-types