07-07 生成器

[TOC]

一 生成器與yield

插圖:惡搞圖49
07-07 生成器

若函數體包含yield關鍵字,再調用函數,並不會執行函數體代碼,得到的返回值即生成器對象

>>> def my_range(start,stop,step=1):
...     print('start...')
...     while start < stop:
...         yield start
...         start+=step
...     print('end...')
... 
>>> g=my_range(0,3)
>>> g
<generator object my_range at 0x104105678>

生成器內置有__iter__和__next__方法,所以生成器本身就是一個迭代器

>>> g.__iter__
<method-wrapper '__iter__' of generator object at 0x1037d2af0>
>>> g.__next__
<method-wrapper '__next__' of generator object at 0x1037d2af0>

插圖:惡搞圖50
07-07 生成器

因而我們可以用next(生成器)觸發生成器所對應函數的執行,

>>> next(g) # 觸發函數執行直到遇到yield則停止,將yield後的值返回,並在當前位置掛起函數
start...
0
>>> next(g) # 再次調用next(g),函數從上次暫停的位置繼續執行,直到重新遇到yield...
1
>>> next(g) # 周而復始...
2
>>> next(g) # 觸發函數執行沒有遇到yield則無值返回,即取值完畢拋出異常結束迭代
end...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

插圖:惡搞圖51
07-07 生成器

既然生成器對象屬於迭代器,那麼必然可以使用for循環迭代,如下:

>>> for i in countdown(3):
...     print(i)
... 
countdown start
3
2
1
Done!

有了yield關鍵字,我們就有了一種自定義迭代器的實現方式。yield可以用於返回值,但不同於return,函數一旦遇到return就結束了,而yield可以保存函數的運行狀態掛起函數,用來返回多次值

插圖:惡搞圖52
07-07 生成器

二 yield表達式應用

在函數內可以採用表達式形式的yield

>>> def eater():
...     print('Ready to eat')
...     while True:
...         food=yield
...         print('get the food: %s, and start to eat' %food)
... 

可以拿到函數的生成器對象持續爲函數體send值,如下

>>> g=eater() # 得到生成器對象
>>> g
<generator object eater at 0x101b6e2b0>
>>> next(e) # 需要事先”初始化”一次,讓函數掛起在food=yield,等待調用g.send()方法爲其傳值
Ready to eat
>>> g.send('包子')
get the food: 包子, and start to eat
>>> g.send('雞腿')
get the food: 雞腿, and start to eat

針對表達式形式的yield,生成器對象必須事先被初始化一次,讓函數掛起在food=yield的位置,等待調用g.send()方法爲函數體傳值,g.send(None)等同於next(g)。

插圖:惡搞圖53
07-07 生成器

​ 我們可以編寫裝飾器來完成爲所有表達式形式yield對應生成器的初始化操作,如下

def init(func):
    def wrapper(*args,**kwargs):
        g=func(*args,**kwargs)
        next(g)
        return g
    return wrapper

@init
def eater():
    print('Ready to eat')
    while True:
        food=yield
        print('get the food: %s, and start to eat' %food)

表達式形式的yield也可以用於返回多次值,即變量名=yield 值的形式,如下

>>> def eater():
...     print('Ready to eat')
...     food_list=[]
...     while True:
...         food=yield food_list
...         food_list.append(food)
... 
>>> e=eater()
>>> next(e)
Ready to eat
[]
>>> e.send('蒸羊羔')
['蒸羊羔']
>>> e.send('蒸熊掌')
['蒸羊羔', '蒸熊掌']
>>> e.send('蒸鹿尾兒')
['蒸羊羔', '蒸熊掌', '蒸鹿尾兒']

插圖:惡搞圖55
07-07 生成器

三 三元表達式、列表生成式、生成器表達式

3.1 三元表達式

三元表達式是python爲我們提供的一種簡化代碼的解決方案,語法如下

res = 條件成立時返回的值 if 條件 else 條件不成立時返回的值

針對下述場景

def max2(x,y):
    if x > y:
        return x
    else:
        return y

res = max2(1,2)

用三元表達式可以一行解決

x=1
y=2
res = x if x > y else y # 三元表達式

插圖:惡搞圖55
07-07 生成器

3.2 列表生成式

列表生成式是python爲我們提供的一種簡化代碼的解決方案,用來快速生成列表,語法如下

[expression for item1 in iterable1 if condition1
for item2 in iterable2 if condition2
...
for itemN in iterableN if conditionN
]

#類似於
res=[]
for item1 in iterable1:
    if condition1:
        for item2 in iterable2:
            if condition2
                ...
                for itemN in iterableN:
                    if conditionN:
                        res.append(expression)

針對下述場景

egg_list=[]
for i in range(10):
    egg_list.append('雞蛋%s' %i)

用列表生成式可以一行解決

egg_list=['雞蛋%s' %i for i in range(10)]

插圖:惡搞圖56
07-07 生成器

3.3 生成器表達式

創建一個生成器對象有兩種方式,一種是調用帶yield關鍵字的函數,另一種就是生成器表達式,與列表生成式的語法格式相同,只需要將[]換成(),即:
07-07 生成器

(expression for item in iterable if condition)

對比列表生成式返回的是一個列表,生成器表達式返回的是一個生成器對象

>>> [x*x for x in range(3)]
[0, 1, 4]
>>> g=(x*x for x in range(3))
>>> g
<generator object <genexpr> at 0x101be0ba0>

對比列表生成式,生成器表達式的優點自然是節省內存(一次只產生一個值在內存中)

>>> next(g)
0
>>> next(g)
1
>>> next(g)
4
>>> next(g) #拋出異常StopIteration

如果我們要讀取一個大文件的字節數,應該基於生成器表達式的方式完成

with open('db.txt','rb') as f:
    nums=(len(line) for line in f)
    total_size=sum(nums) # 依次執行next(nums),然後累加到一起得到結果=

插圖:惡搞圖57
07-07 生成器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章