迭代器是可以迭代的對象。 在本教程中,您將了解迭代器的工作原理,以及如何使用__iter__和__next__方法構(gòu)建自己的迭代器。
迭代器在Python中無處不在。 它們優(yōu)雅地實現(xiàn)在循環(huán),推導(dǎo),生成器等中,但隱藏在明顯的視覺中。
Python中的迭代器只是一個可以迭代的對象。一個將一次返回數(shù)據(jù)的對象或一個元素。
從技術(shù)上講,Python迭代器對象必須實現(xiàn)兩個特殊的方法__iter__()和__next__(),統(tǒng)稱為迭代器協(xié)議。
如果我們從中獲取一個迭代器,那么一個對象被稱為iterable。 大多數(shù)Python中的內(nèi)置容器是列表,元組,字符串等都是可迭代的。
iter()函數(shù)(這又調(diào)用__iter__()方法)返回一個迭代器。
使用next()函數(shù)來手動遍歷迭代器的所有項目。當(dāng)?shù)竭_結(jié)束,沒有更多的數(shù)據(jù)要返回時,它將會引發(fā)StopIteration。 以下是一個例子。
# define a list
my_list = [4, 7, 0, 3]
# get an iterator using iter()
my_iter = iter(my_list)
## iterate through it using next()
#prints 4
print(next(my_iter))
#prints 7
print(next(my_iter))
## next(obj) is same as obj.__next__()
#prints 0
print(my_iter.__next__())
#prints 3
print(my_iter.__next__())
## This will raise error, no items left
next(my_iter)
更優(yōu)雅的自動迭代方式是使用for循環(huán)。 使用for循環(huán)可以迭代任何可以返回迭代器的對象,例如列表,字符串,文件等。
>>> for element in my_list:
... print(element)
...
4
7
0
3
在上面的例子中看到的,for循環(huán)能夠自動通過列表迭代。
事實上,for循環(huán)可以迭代任何可迭代對象。我們來仔細看一下在Python中是如何實現(xiàn)for循環(huán)的。
for element in iterable:
# do something with element
實際上它是以類似下面的方式來實現(xiàn)的 -
# create an iterator object from that iterable
iter_obj = iter(iterable)
# infinite loop
while True:
try:
# get the next item
element = next(iter_obj)
# do something with element
except StopIteration:
# if StopIteration is raised, break from loop
break
所以在for的內(nèi)部,for循環(huán)通過在可迭代的對象上調(diào)用iter()來創(chuàng)建一個迭代器對象iter_obj。
有意思的是,這個for循環(huán)實際上是一個無限循環(huán)~..~。
在循環(huán)中,它調(diào)用next()來獲取下一個元素,并使用該值執(zhí)行for循環(huán)的主體。 在所有對象耗盡后,引發(fā)StopIteration異常,內(nèi)部被捕獲從而結(jié)束循環(huán)。請注意,任何其他類型的異常都將正常通過。
構(gòu)建迭代器在Python中很容易。只需要實現(xiàn)__iter__()和__next__()方法。
__iter__()方法返回迭代器對象本身。如果需要,可以執(zhí)行一些初始化。
__next__()方法必須返回序列中的下一個項目(數(shù)據(jù)對象)。 在到達結(jié)束后,并在隨后的調(diào)用中它必須引發(fā)StopIteration異常。
在這里,我們展示一個例子,在每次迭代中給出下一個2的幾次方。 次冪指數(shù)從零開始到用戶設(shè)定的數(shù)字。
class PowTwo:
"""Class to implement an iterator
of powers of two"""
def __init__(self, max = 0):
self.max = max
def __iter__(self):
self.n = 0
return self
def __next__(self):
if self.n <= self.max:
result = 2 ** self.n
self.n += 1
return result
else:
raise StopIteration
現(xiàn)在可以創(chuàng)建一個迭代器,并通過它迭代如下 -
>>> a = PowTwo(4)
>>> i = iter(a)
>>> next(i)
1
>>> next(i)
2
>>> next(i)
4
>>> next(i)
8
>>> next(i)
16
>>> next(i)
Traceback (most recent call last):
...
StopIteration
也可以使用for循環(huán)迭代那些迭代器類。
>>> for i in PowTwo(5):
... print(i)
...
1
2
4
8
16
32
迭代器對象中的項目不必都是可耗盡的,可以是無限迭代器(永遠不會結(jié)束)。 處理這樣的迭代器時一定要小心。
下面是用來演示無限迭代器的一個簡單的例子。
內(nèi)置的函數(shù)iter()可以用兩個參數(shù)來調(diào)用,其中第一個參數(shù)必須是可調(diào)用對象(函數(shù)),而第二個參數(shù)是標頭。迭代器調(diào)用此函數(shù),直到返回的值等于指定值。
>>> int()
0
>>> inf = iter(int,1)
>>> next(inf)
0
>>> next(inf)
0
可以看到,int()函數(shù)總是返回0,所以將它作為iter(int,1)傳遞將返回一個調(diào)用int()的迭代器,直到返回值等于1。這從來沒有發(fā)生,所以這樣就得到一個無限迭代器。
我們也可以建立自己的無限迭代器。 以下迭代器理論上將返回所有奇數(shù)。
class InfIter:
"""Infinite iterator to return all
odd numbers"""
def __iter__(self):
self.num = 1
return self
def __next__(self):
num = self.num
self.num += 2
return num
示例運行如下 -
>>> a = iter(InfIter())
>>> next(a)
1
>>> next(a)
3
>>> next(a)
5
>>> next(a)
7
當(dāng)?shù)@些類型的無限迭代器時,請注意指定終止條件。
使用迭代器的優(yōu)點是它們可以節(jié)省資源。 如上所示,我們可以獲得所有奇數(shù),而不將整個系統(tǒng)存儲在內(nèi)存中。理論上,可以在有限的內(nèi)存中計算有無限的項目。