首先,廖雪峰老師的教程中解釋了迭代器和生成器,這篇文章只是補充和我個人的總結。
可以直接作用于for循環的對象統稱為可迭代對象(Iterable)。
可以被next()函數調用并不斷返回下一個值的對象稱為迭代器(Iterator)。
所有的Iterable均可以通過內置函數iter()來轉變為Iterator。
對迭代器來講,有一個__next__()就夠了。在你使用for 和 in 語句時,程序就會自動調用即將被處理的對象的迭代器對象,然后使用它的__next__()方法,直到監測到一個StopIteration異常。
>>> L = [1,2,3]>>> [x**2 for x in L][1, 4, 9]>>> next(L)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'list' object is not an iterator>>> I=iter(L)>>> next(I)1>>> next(I)2>>> next(I)3>>> next(I)Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration上面例子中,列表L可以被for進行循環但是不能被內置函數next()用來查找下一個值,所以L是Iterable。
L通過iter進行包裝后設為I,I可以被next()用來查找下一個值,所以I是Iterator。
題外話:
__iter__()方法,所以list對象內部一定存在方法__iter__()__next__()方法,所以list對象內部一定不存在方法__next__(),但是Itrator中一定存在這個方法。for循環內部事實上就是先調用iter()把Iterable變成Iterator在進行循環迭代的。
>>> L = [4,5,6]>>> I = L.__iter__()>>> L.__next__()Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'list' object has no attribute '__next__'>>> I.__next__()4>>> from collections import Iterator, Iterable>>> isinstance(L, Iterable)True>>> isinstance(L, Iterator)False>>> isinstance(I, Iterable)True>>> isinstance(I, Iterator)True>>> [x**2 for x in I] [25, 36]Iterator繼承自Iterable,從下面的測試中可以很方便的看到Iterator包含__iter__()和__next__()方法,而Iteratble僅僅包含__iter__()。
>>> from collections import Iterator, Iterable>>> help(Iterator)Help on class Iterator:class Iterator(Iterable) | Method resolution order: | Iterator | Iterable | builtins.object |**注解:從這里可以看出Iterable繼承自object, Iterator繼承自Iterable。 | Methods defined here: | | __iter__(self) | | __next__(self) | Return the next item from the iterator. When exhausted, raise StopIteration......>>> help(Iterable)Help on class Iterable:class Iterable(builtins.object) | Methods defined here: | | __iter__(self)......如果我們自己定義迭代器,只要在類里面定義一個 __iter__() 函數,用它來返回一個帶 __next__() 方法的對象就夠了。
直接上代碼
class Iterable: def __iter__(self): return Iterator()class Iterator: def __init__(self): self.start=-1 def __next__(self): self.start +=2 if self.start >10: raise StopIteration return self.startI = Iterable()for i in I: PRint(i)上面的代碼實現的是找到10以內的奇數,代碼中的類名可以隨便取,不是一定需要使用我上面提供的類名的。
如果在Iterator的__next__方法中沒有實現StopIteration異常,那么則是表示的全部奇數,那么需要在調用的時候設置退出循環的條件。
class Iterable: def __iter__(self): return Iterator()class Iterator: def __init__(self): self.start=-1 def __next__(self): self.start +=2 return self.startI = Iterable()for count, i in zip(range(5),I): #也可以用內置函數enumerate來實現計數工作。 print(i)我們通過range來實現打印多少個元素,這里表示打印5個元素,返回結果和上面一致。
當然,我們可以把這兩個類合并在一起,這樣實現程序的簡練。
最終版本如下
class Iterable: def __iter__(self): return self def __init__(self): self.start=-1 def __next__(self): self.start +=2 if self.start >10: raise StopIteration return self.startI = Iterable()for i in I: print(i)迭代器是一次性消耗品,使用完了以后就空了,請看。
>>> L=[1,2,3]>>> I=iter(L)>>> for i in I:... print(i, end='-')...1-2-3->>>next(I)Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration當循環以后就殆盡了,再次使用調用時會引發StopIteration異常。
我們想通過直接賦值的形式把迭代器保存起來,可以下次使用。
但是通過下面的范例可以看出來,根本不管用。
>>> I=iter(L)>>> J=I>>> next(I)1>>> next(J)2>>> next(I)3>>> next(J)Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration那怎么樣才能達到我們要的效果呢?
我們需要使用copy包中的deepcopy了,請看下面:
>>> import copy>>> I=iter(L)>>> J=copy.deepcopy(I)>>> next(I)1>>> next(I)2>>> next(J)1補充:迭代器不能向后移動, 不能回到開始。
所以需要做一些特殊的事情才能實現向后移動等功能。
以上代碼均在Python 3.4 中測試通過。
日志:
新聞熱點
疑難解答