I have read my materials, which tell that a Python iterator must have both __iter__
and __next__
methods, but an iterable just needs __iter__
.
I check a list and find it has no __next__
method. When using iter()
on it, it will become an iterator. Does this mean that iter()
will add a __next__
method to a list to convert it to an iterator?
If yes, how does this happen?
No. iter
returns an iterator, it does not convert the list into an iterator. It doesn't modify the list at all, and certainly, the list does not get a __next__
method.
>>> x = [1,2]
>>> it = iter(x)
>>> it
<list_iterator object at 0x101c021d0>
>>> x.__next__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__next__'
>>>
Lists are iterables, not iterators. They implement a __iter__
method, thus they are iterable:
>>> x.__iter__
<method-wrapper '__iter__' of list object at 0x101bcf248>
But not __next__
, thus they are not iterators:
>>> next(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not an iterator
Iterators themselves are iterable, by definition, since they implement __iter__
as well. Consider:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> it = iter(x)
>>> it
<list_iterator object at 0x101c02358>
>>> it.__iter__
<method-wrapper '__iter__' of list_iterator object at 0x101c02358>
Most iterators should simply return themselves when you use iter
on them:
>>> it2 = iter(it)
>>> it, it2
(<list_iterator object at 0x101c02358>, <list_iterator object at 0x101c02358>)
>>> it is it2
True
>>>
Indeed, this is a requirement of the iterator protocol:
"Iterators are required to have an
__iter__()
method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted."
Note, again, they are the same iterator:
>>> next(it)
1
>>> next(it2)
2
>>> next(it)
3
>>> next(it)
4
>>> next(it2)
5
>>> list(it)
[6, 7, 8, 9]
>>> next(it2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
So an iterator implements __iter__
and __next__
, an iterable just means that it implements __iter__
. What is returned by __iter__
is an iterator, so that must implement __next__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With