回顧:
- 生成器就是含有yield操作的函數(shù)前计,生成器函數(shù)的返回值就是一個(gè)迭代器(generator object)
主要內(nèi)容:
- 外部如何給生成器發(fā)送一個(gè)值,臨時(shí)按需變化迭代生成值垃杖。(send方法)
- 生成器返回的迭代器男杈,一般只能使用一次〉鞣可采用內(nèi)在的異常機(jī)制對(duì)第二次調(diào)用報(bào)錯(cuò)伶棒。
- 生成器“對(duì)象”(生成器產(chǎn)生的迭代器對(duì)象)的close方法示例
- 個(gè)人理解與總結(jié)
5.1 send 函數(shù)
- 錯(cuò)誤的例子
>>> def simpleGen(value): # 定義了一個(gè)簡(jiǎn)單的生成器
... while True:
... yield value
...
>>> gen = simpleGen(42);
>>> print(gen.__next__(),gen.__next__())
42 42
>>> gen.send(10);
42
>>> print(gen.__next__(),gen.__next__())
42 42
yield 此處是一個(gè)語(yǔ)句。然而我們需要在生成器函數(shù)中對(duì)gen.send(10);
進(jìn)行響應(yīng),那么我們就要使用yield 表達(dá)式彩库, yield 不再是語(yǔ)句肤无,而是表達(dá)式!:铡舅锄!
- 正確的例子
#正確的例子
>>> def simpleGen(value):
... new = None;
... i = 0;
... while True:
... i +=1
... print('In Func (before):',i, new, value)
... new = yield value
... print('In Func (after):',i, new, value)
... value = new;
...
>>> gen = simpleGen(42); # nothing happend
>>> print(gen.__next__()) # called 1st, get 42
In Func (before): 1 None 42
42
>>> print(gen.__next__()) # called 2nd, get None, this is important!
In Func (after): 1 None 42
In Func (before): 2 None None
None
>>> print(gen.send(10)); # called 3rd, get what you set
In Func (after): 2 10 None
In Func (before): 3 10 10
10
>>> print(gen.__next__()) # called 4th, get None, this is important!
In Func (after): 3 None 10
In Func (before): 4 None None
None
此處應(yīng)該有結(jié)論:
- 構(gòu)造函數(shù)啥都不干,除了賦值
- send函數(shù)和__next__函數(shù)一樣司忱,都喚醒了沉睡中的yield表達(dá)式
- yield表達(dá)式的值在正常情況下是None
- 最終更佳的例子(處理None)
>>> def simpleGen(value):
... while True:
... new = yield value
... if new is not None:
... value = new;
...
>>> gen = simpleGen(42); # nothing happend
>>> print(gen.__next__()) # called 1st, get 42
42
>>> print(gen.__next__()) # called 2nd, get 42
42
>>> print(gen.send(10)); # called 3rd, get what you set
10
>>> print(gen.__next__()) # called 4th, also get what you set
10
5.2 throw 方法
這個(gè)很簡(jiǎn)單,就是通過(guò)語(yǔ)句畴蹭,然生成器內(nèi)部產(chǎn)生異常坦仍,注意異常報(bào)出的位置(yield 語(yǔ)句)
>>> def simpleGen(value):
... while True:
... new = yield value
... if new is not None:
... value = new;
...
>>> gen = simpleGen(42); # nothing happend
>>> print(gen.__next__()) # called 1st, get 42
42
>>> gen.throw(StopIteration)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in simpleGen ## 注意異常報(bào)出的位置(yield 語(yǔ)句)。
StopIteration
5.3 close 語(yǔ)句
顯然 throw方法產(chǎn)生的異常并沒(méi)有在內(nèi)部處理掉叨襟,直接立馬傳遞到上一層了繁扎。我們能否設(shè)置下迭代生成器的終止,也就是不能產(chǎn)生下一個(gè)了糊闽。那就是無(wú)參數(shù)的close方法梳玫。
>>> def simpleGen(value):
... while True:
... new = yield value
... if new is not None:
... value = new;
...
>>> gen = simpleGen(42); # nothing happend
>>> print(gen.__next__()) # called 1st, get 42
42
>>> gen.close() # 終止了這個(gè)迭代器,但是沒(méi)有異常
>>> print(gen.__next__()) # 繼續(xù)取值右犹,便產(chǎn)生了StopIteration的異常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
5.4 小結(jié)下:
學(xué)習(xí)迭代器和生成器有一兩天了提澎,自己也編程實(shí)現(xiàn)了大部分的功能。其目的都是為了高效的迭代循環(huán)而生的念链。那么循環(huán)的處理方式有如下幾種:
- 直接采用元組盼忌、列表、字符串
- 利用內(nèi)置的迭代器(xrange函數(shù)等)
- 自己構(gòu)造類(迭代器)(__next__,__iter__)
- 使用yield函數(shù)(生成器)(yield掂墓,__next__,send, throw, close)
感受谦纱,在表達(dá)形式上,Python確實(shí)和c君编、c++語(yǔ)言有較大差別跨嘉。但是,這種迭代器的核心思想在C吃嘿、C++中是以指針祠乃、引用等復(fù)雜的方式實(shí)現(xiàn)的梦重,Python肯定是犧牲了一定的計(jì)算效率將其封裝成為了迭代器和生成器,帶來(lái)的好處是指針和內(nèi)存的安全跳纳,編程效率的提升忍饰,所以python是一門很好的科學(xué)計(jì)算語(yǔ)言,使得開發(fā)者能夠注重算法層面的開發(fā)寺庄,而不是指針內(nèi)存等煩人細(xì)節(jié)艾蓝。