I have read my materials, which tell that a python iterator must have both __iter__ and __next__ method, 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. This means 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
>>> x.__next__
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'list' object has no attribute '__next__'
>>>
Lists are iterables, not iterators. They implement a __iter__ method, thus they are iterable:
>>> x.__iter__
But not __next__, thus they are not iterators:
>>> next(x)
Traceback (most recent call last):
File "", line 1, in
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
>>> it.__iter__
Most iterators should simply return themselves when you use iter on them:
>>> it2 = iter(it)
>>> it, it2
(, )
>>> it is it2
True
>>>
"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 "", line 1, in
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__.