国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python生成器

2019-11-14 11:47:20
字體:
來源:轉載
供稿:網友

從句法上講,生成器是一個帶yield 語句的函數。一個函數或者子程序只返回一次,但一個生成器能暫停執行并返回一個中間的結果——那就是yield 語句的功能, 返回一個值給調用者并暫停執行。當生成器的next()方法被調用的時候,它會準確地從離開地方繼續。當到達一個真正的返回或者函數結束沒有更多的值返回(當調用next()),一個StopIteration 異常就會拋出。簡單的生成器:

def simple_gen(): yield 1 yield '2 --> punch!'

調用:

>>> myG = simple_gen()>>> myG.next()1>>> myG.next()'2 --> punch!'>>> myG.next()Traceback (most recent call last):File "", line 1, in ?myG.next() StopIteration

由于python 的for 循環有next()調用和對StopIteration 的處理,使用一個for 循環而不是手動迭代穿過一個生成器(或者那種事物的迭代器)總是要簡潔漂亮得多:

>>> for eachItem in simple_gen():... PRint eachItem...1'2 --> punch!'

下面的例子,將要創建一個帶序列并從那個序列中返回一個隨機元素的隨機迭代器,每個返回的元素將從那個隊列中消失,像一個list.pop()和random.choice()的結合的歸類:

from random import randintdef rand_gen(aList): while len(aList) > 0: yield aList.pop(randint(0, len(aList)))

調用:

>>> for item in rand_gen(['rock', 'paper', 'scissors']):... print item...scissorsrockpaper

除了next()來獲得下個生成的值,用戶可以將值回送給生成器[send()],在生成器中拋出異常,以及要求生成器退出[close()]

def counter(start_at=0): count = start_at while True: val = (yield count) if val is not None: print 'reset here' count = val else: count += 1 if count == 4: raise Exception('aha')

send調用:

count = counter(5)print count.next()print count.next()count.send(9)print count.next()

輸出:

56reset here10

close調用:

count = counter(5)print count.next()print count.next()count.close()count.next() # raise StopIteration here

輸出:

56Traceback (most recent call last): File "/home/zhangjun/workspace/try/src/try.py", line 16, in <module> count.next() # raise StopIteration hereStopIteration

拋出異常調用:

count = counter(3)print count.next()print count.next()# raise Exception here

輸出:

3Traceback (most recent call last): File "/home/zhangjun/workspace/try/src/try.py", line 14, in <module> print count.next()# raise Exception here File "/home/zhangjun/workspace/try/src/try.py", line 11, in counter raise Exception('aha')Exception: aha
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 紫云| 芒康县| 枣强县| 利津县| 防城港市| 宜阳县| 贺州市| 汝南县| 石景山区| 太康县| 织金县| 遵义县| 方山县| 台山市| 宜阳县| 海门市| 翼城县| 辽中县| 孝义市| 博罗县| 哈密市| 平武县| 辽阳县| 南平市| 鄂伦春自治旗| 行唐县| 章丘市| 伊吾县| 乌拉特前旗| 玉环县| 乐山市| 谷城县| 潮安县| 汝城县| 平武县| 阿巴嘎旗| 尼玛县| 台南县| 新干县| 亚东县| 霍城县|