新聞中心
Python 提供了一個生成器來創(chuàng)建自己的迭代器函數(shù)。 生成器是一種特殊類型的函數(shù),它不返回單個值,而是返回一個包含一系列值的迭代器對象。 在生成器函數(shù)中,使用yield語句,而不是返回語句。 下面是一個簡單的生成器函數(shù)。

創(chuàng)新互聯(lián)建站是一家以網(wǎng)絡技術公司,為中小企業(yè)提供網(wǎng)站維護、成都網(wǎng)站建設、網(wǎng)站設計、網(wǎng)站備案、服務器租用、域名申請、軟件開發(fā)、小程序開發(fā)等企業(yè)互聯(lián)網(wǎng)相關業(yè)務,是一家有著豐富的互聯(lián)網(wǎng)運營推廣經(jīng)驗的科技公司,有著多年的網(wǎng)站建站經(jīng)驗,致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個面向全國乃至全球的業(yè)務窗口:建站歡迎來電:18982081108
Example: Generator Function
def mygenerator():
print('First item')
yield 10
print('Second item')
yield 20
print('Last item')
yield 30 在上面的例子中,mygenerator()函數(shù)是一個生成器函數(shù)。它使用yield而不是 return 關鍵字。 因此,這將在每次調(diào)用yield關鍵字時返回該值。但是,您需要為此函數(shù)創(chuàng)建一個迭代器,如下所示。
Example: next()
>>> gen = mygenerator()
>>> next(gen)
First item
10
>>> next(gen)
Second item
20
>>> next(gen)
Last item
30 生成器函數(shù)不能包含return關鍵字。如果包含它,那么它將終止函數(shù)。 yield和return的區(qū)別在于yield返回值并暫停執(zhí)行,同時保持內(nèi)部狀態(tài),而return語句返回值并終止函數(shù)的執(zhí)行。
以下生成器函數(shù)包含 return 關鍵字。
Example: return in Generator Function
def mygenerator():
print('First item')
yield 10
return
print('Second item')
yield 20
print('Last item')
yield 30 現(xiàn)在,如下所示執(zhí)行上述功能。
Example: Generator Function
>>> gen = mygenerator()
>>> next(gen)
First item
10
>>> next(gen)
Traceback (most recent call last):
File "", line 1, in
it.__next__()
StopIteration 如您所見,上面的生成器在獲取第一項后停止執(zhí)行,因為 return 關鍵字是在yield獲取第一項后使用的。
用于具有生成器功能的循環(huán)
生成器函數(shù)也可以使用 for循環(huán)。
Example: Use For Loop with Generator Function
def get_sequence_upto(x):
for i in range(x):
yield i 如上圖所示,get_sequence_upto函數(shù)使用了yield關鍵字。 發(fā)電機的調(diào)用就像正常功能一樣。 然而,當遇到yield關鍵字時,其執(zhí)行被暫停。這將迭代器流的第一個值發(fā)送到調(diào)用環(huán)境。但是,局部變量及其狀態(tài)保存在內(nèi)部。
上面的生成器函數(shù)get_sequence_upto()可以如下調(diào)用。
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 "", line 1, in
it.__next__()
StopIteration 當向迭代器對象發(fā)出 next() 時,該函數(shù)恢復。當next()遇到StopIteration錯誤時,該功能最終終止。
在下面的例子中,函數(shù)square_of_sequence()充當一個生成器。它在每次調(diào)用 next() 時連續(xù)產(chǎn)生一個數(shù)字的平方。
Example:
def square_of_sequence(x):
for i in range(x):
yield i*i 下面的腳本顯示了如何調(diào)用上面的生成器函數(shù)。
gen=square_of_sequence(5)
while True:
try:
print ("Received on next(): ", next(gen))
except StopIteration:
break 上面的腳本使用try..except塊來處理StopIteration錯誤。一旦捕捉到StopIteration錯誤,它將中斷 while循環(huán)。
Output
Received on next(): 0
Received on next(): 1
Received on next(): 4
Received on next(): 9
Received on next(): 16 我們可以使用 for循環(huán)遍歷生成器上的元素。在這種情況下,next()函數(shù)被隱式調(diào)用,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 還提供了一個生成器表達式,這是定義簡單生成器函數(shù)的一種更短的方式。生成器表達式是匿名生成器函數(shù)。以下是square_of_sequence()函數(shù)的生成器表達式。
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循環(huán)。
生成器表達式也可以在函數(shù)中傳遞。它應該不帶括號傳遞,如下所示。
Example: Passing Generator Function
>>> import math
>>> sum(x*x for x in range(5))
30 在上面的例子中,一個沒有括號的生成器表達式被傳遞到內(nèi)置函數(shù)sum中。***
本文題目:Python生成器函數(shù)
本文URL:http://m.fisionsoft.com.cn/article/dhhppee.html


咨詢
建站咨詢
