Reputation: 1072
Several places online, including answers in Stack Overflow (such as this, this & this), mention that Python iterators must implement both the __next__
method (which I understand) and the __iter__
method. These places rightfully conclude that all iterators are also iterables. Even PyCharm issues a type warning if a variable which is annotated to be a typing.Iterator
doesn't implement that __iter__
method.
Contrary to those, the official Python tutorial section on iterators only mentions the need for a __next__
method:
The function returns an iterator object that defines the method
__next__()
which accesses elements in the container one at a time
So my question is: do Python iterators formally need to be iterables on their own? I personally don't understand why this should be true, and why we can't fully separate the requirements for an Iterable
and an Iterator
.
Upvotes: 5
Views: 142
Reputation: 281046
That's the tutorial. It glosses over things. If you check the data model documentation, you'll see an explicit requirement that iterators support __iter__
:
The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:
iterator.__iter__()
Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.
iterator.__next__()
...
Python easily could have been designed to make iterators non-iterable, the way Java did, but that would have been counterproductive. Iterating over iterators is extremely common and standard in Python, and it was always intended to be.
Having to stick some kind of iterwrapper
around iterators in every for
loop would be like having to stick some kind of addablewrapper
around your integers every time you wanted to add two integers.
Upvotes: 4