Python 提供了一個生成器來創建自己的迭代器函數。 生成器是一種特殊類型的函數,它不返回單個值,而是返回一個包含一系列值的迭代器對象。 在生成器函數中,使用yield
語句,而不是返回語句。 下面是一個簡單的生成器函數。
Example: Generator Function
def mygenerator():
print('First item')
yield 10
print('Second item')
yield 20
print('Last item')
yield 30
在上面的例子中,mygenerator()
函數是一個生成器函數。它使用yield
而不是 return 關鍵字。 因此,這將在每次調用yield
關鍵字時返回該值。但是,您需要為此函數創建一個迭代器,如下所示。
Example: next()
>>> gen = mygenerator()
>>> next(gen)
First item
10
>>> next(gen)
Second item
20
>>> next(gen)
Last item
30
生成器函數不能包含return
關鍵字。如果包含它,那么它將終止函數。 yield
和return
的區別在于yield
返回值并暫停執行,同時保持內部狀態,而return
語句返回值并終止函數的執行。
以下生成器函數包含 return 關鍵字。
Example: return in Generator Function
def mygenerator():
print('First item')
yield 10
return
print('Second item')
yield 20
print('Last item')
yield 30
現在,如下所示執行上述功能。
Example: Generator Function
>>> gen = mygenerator()
>>> next(gen)
First item
10
>>> next(gen)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
it.__next__()
StopIteration
如您所見,上面的生成器在獲取第一項后停止執行,因為 return 關鍵字是在yield
獲取第一項后使用的。
用于具有生成器功能的循環
生成器函數也可以使用 for
循環。
Example: Use For Loop with Generator Function
def get_sequence_upto(x):
for i in range(x):
yield i
如上圖所示,get_sequence_upto
函數使用了yield
關鍵字。 發電機的調用就像正常功能一樣。 然而,當遇到yield
關鍵字時,其執行被暫停。這將迭代器流的第一個值發送到調用環境。但是,局部變量及其狀態保存在內部。
上面的生成器函數get_sequence_upto()
可以如下調用。
Example: Calling Generator Function
>>> seq = get_sequence_upto(5)
>>> next(seq)
0
>>> next(seq)
1
>>> next(seq)
2
>>> next(seq)
3
>>> next(seq)
4
>>> next(seq)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
it.__next__()
StopIteration
當向迭代器對象發出 next() 時,該函數恢復。當next()
遇到StopIteration
錯誤時,該功能最終終止。
在下面的例子中,函數square_of_sequence()
充當一個生成器。它在每次調用 next() 時連續產生一個數字的平方。
Example:
def square_of_sequence(x):
for i in range(x):
yield i*i
下面的腳本顯示了如何調用上面的生成器函數。
gen=square_of_sequence(5)
while True:
try:
print ("Received on next(): ", next(gen))
except StopIteration:
break
上面的腳本使用try..except
塊來處理StopIteration
錯誤。一旦捕捉到StopIteration
錯誤,它將中斷 while
循環。
Output
Received on next(): 0
Received on next(): 1
Received on next(): 4
Received on next(): 9
Received on next(): 16
我們可以使用 for
循環遍歷生成器上的元素。在這種情況下,next()
函數被隱式調用,StopIteration
也被自動處理。
Example: Generator with the For Loop
squres = square_of_sequence(5)
for sqr in squres:
print(sqr)
Output
0
1
4
9
16
*Note:*One of the advantages of the generator over the iterator is that elements are generated dynamically. Since the next item is generated only after the first is consumed, it is more memory efficient than the iterator. *## 生成器表達式
Python 還提供了一個生成器表達式,這是定義簡單生成器函數的一種更短的方式。生成器表達式是匿名生成器函數。以下是square_of_sequence()
函數的生成器表達式。
Example: Generator Expression
>>> squres = (x*x for x in range(5))
>>> print(next(squre))
0
>>> print(next(squre))
1
>>> print(next(squre))
4
>>> print(next(squre))
9
>>> print(next(squre))
16
在上面的例子中,(x*x for x in range(5))
是一個生成器表達式。表達式的第一部分是yield
值,第二部分是帶有集合的 for
循環。
生成器表達式也可以在函數中傳遞。它應該不帶括號傳遞,如下所示。
Example: Passing Generator Function
>>> import math
>>> sum(x*x for x in range(5))
30
在上面的例子中,一個沒有括號的生成器表達式被傳遞到內置函數sum
中。***