問(wèn)題描述
我已經(jīng)閱讀了我的材料,其中告訴 Python 迭代器必須同時(shí)具有 __iter__
和 __next__
方法,但迭代器只需要 __iter__
.
I have read my materials, which tell that a Python iterator must have both __iter__
and __next__
methods, but an iterable just needs __iter__
.
我檢查了一個(gè)列表,發(fā)現(xiàn)它沒(méi)有 __next__
方法.當(dāng)在其上使用 iter()
時(shí),它將成為一個(gè)迭代器.這是否意味著 iter()
會(huì)在列表中添加一個(gè) __next__
方法以將其轉(zhuǎn)換為迭代器?
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?
如果是,這是怎么發(fā)生的?
If yes, how does this happen?
推薦答案
沒(méi)有.iter
返回一個(gè)迭代器,它不會(huì)將列表轉(zhuǎn)換為迭代器.它根本不修改列表,當(dāng)然,列表沒(méi)有得到 __next__
方法.
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__'
>>>
列表是iterables,而不是iterators.它們實(shí)現(xiàn)了一個(gè) __iter__
方法,因此它們是可迭代的:
Lists are iterables, not iterators. They implement a __iter__
method, thus they are iterable:
>>> x.__iter__
<method-wrapper '__iter__' of list object at 0x101bcf248>
但不是 __next__
,因此它們不是迭代器:
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
根據(jù)定義,迭代器本身是可迭代的,因?yàn)樗鼈円矊?shí)現(xiàn)了 __iter__
.考慮:
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>
大多數(shù)迭代器應(yīng)該在你對(duì)它們使用iter
時(shí)簡(jiǎn)單地返回它們自己:
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
>>>
確實(shí),這是迭代器協(xié)議的要求:
"迭代器必須有一個(gè)返回的 __iter__()
方法迭代器對(duì)象本身,因此每個(gè)迭代器也是可迭代的,并且可能是在接受其他迭代的大多數(shù)地方使用."
"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."
再次注意,它們是同一個(gè)迭代器:
>>> 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
所以一個(gè)iterator實(shí)現(xiàn)了__iter__
和__next__
,一個(gè)iterable僅僅意味著它實(shí)現(xiàn)了__iter__代碼>.返回
__iter__
是一個(gè)迭代器,所以它必須實(shí)現(xiàn) __next__
.
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__
.
這篇關(guān)于內(nèi)置函數(shù) iter() 如何將 Python 列表轉(zhuǎn)換為迭代器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!