How to understand the key word 'yield'

Iterables

mylist is an iterable. When you use a list comprehension, you create a list, and so an iterable:

>>> mylist = [x*x for x in range(3)]
>>> for i in mylist:
...    print(i)
0
1
4

Everything you can use "for... in..." on is an iterable; lists, strings, files...
These iterables are handy because you can read them as much as you wish, but you store all the values in memory and this is not always what you want when you have a lot of values.

Generators

Generators are iterators, a kind of iterable you can only iterate over once. Generators do not store all the values in memory, they generate the values on the fly:

>>> for i in mygenerator:
...    print(i)
0
1
4

It is just the same except you used () instead of []. BUT, you cannot perform for i in mygenerator a second time since generators can only be used once: they calculate 0, then forget about it and calculate 1, and end calculating 4, one by one.

Yield

yield is a keyword that is used like return, except the function will return a generator.

>>> def createGenerator():
...    mylist = range(3)
...    for i in mylist:
...        yield i*i
...
>>> mygenerator = createGenerator() # create a generator
>>> print(mygenerator) # mygenerator is an object!
<generator object createGenerator at 0xb7555c34>
>>> for i in mygenerator:
...     print(i)
0
1
4

Here it's a useless example, but it's handy when you know your function will return a huge set of values that you will only need to read once.

To master yield, you must understand that when you call the function, the code you have written in the function body does not run. The function only returns the generator object, this is a bit tricky :-)

Then, your code will be run each time the for uses the generator.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子憨琳,更是在濱河造成了極大的恐慌茂蚓,老刑警劉巖音诈,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伺通,死亡現(xiàn)場(chǎng)離奇詭異必怜,居然都是意外死亡唆迁,警方通過查閱死者的電腦和手機(jī)鸭丛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來唐责,“玉大人鳞溉,你說我怎么就攤上這事∈蟾纾” “怎么了熟菲?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)朴恳。 經(jīng)常有香客問我抄罕,道長(zhǎng),這世上最難降的妖魔是什么于颖? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任贞绵,我火速辦了婚禮,結(jié)果婚禮上恍飘,老公的妹妹穿的比我還像新娘榨崩。我一直安慰自己,他們只是感情好章母,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布母蛛。 她就那樣靜靜地躺著,像睡著了一般乳怎。 火紅的嫁衣襯著肌膚如雪彩郊。 梳的紋絲不亂的頭發(fā)上前弯,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音秫逝,去河邊找鬼恕出。 笑死,一個(gè)胖子當(dāng)著我的面吹牛违帆,可吹牛的內(nèi)容都是我干的浙巫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼刷后,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼的畴!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起尝胆,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤丧裁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后含衔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體煎娇,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年贪染,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缓呛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡抑进,死狀恐怖强经,靈堂內(nèi)的尸體忽然破棺而出睡陪,到底是詐尸還是另有隱情寺渗,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布兰迫,位于F島的核電站信殊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏汁果。R本人自食惡果不足惜涡拘,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望据德。 院中可真熱鬧鳄乏,春花似錦、人聲如沸棘利。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽善玫。三九已至水援,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蜗元。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工或渤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奕扣。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓薪鹦,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親成畦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子距芬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容