C#的yield已經忘得差不多了。又遇到python的yield。
iterator
def testYield(): print 'yield1' m = yield 1 print 'm =' , m print 'yield2' yield 5 for a in testYield(): print 'test' result = testYield() result.send('test') print list(result)
OUTPUT:
yield1
test
m = None
yield2
test
File "C:\pytest\Sele\Generator.py", line 36, in <module>
result.send('test')
TypeError: can't send non-None value to a just-started generator
[Finished in 0.2s with exit code 1]
參考這里面的說:
http://www.jb51.net/article/15717.htm
send(something) and next()
第一次調用,send不能傳非空值,不知道為什么,以后再搞明白
yield 在函數里出現,說明這個函數是generator,生成器,會被區別對待。
舉個栗子~
def testYield(): m = yield 1 print type(m) print m m2 = yield 5 yield 88 result = testYield() print result.next() print result.send('fighting')
result.next()
print result.next()
# print list(result)
print result.next() A:給我做一塊蛋糕, 並打包
噠噠噠,進入函數工廠
計算機: 好的,做好了。給你1
print.send('fighting') B:給我做一塊蛋糕,讓‘fighting’來做,並打包
噠噠噠,進入函數工廠
計算機:好的,這就讓fighting去做。給你5
result.next()C:給我做一塊蛋糕, 並打包
噠噠噠,進入函數工廠
計算機:好的,做好了。給你88,哎?人呢?
print result.next() D:給我做一塊蛋糕, 並打包
噠噠噠,進入函數工廠
計算機: 沒有原料了,做不出來蛋糕了,停止售賣 stop iteration
print list(result) E:看看你們能做啥
噠噠噠,進入函數工廠
計算機:我們空了 輸出【】
正兒八經的輸出是:
1
<type 'str'>
fighting
5
File "C:\pytest\Sele\Generator.py", line 39, in <module>
print result.next()
StopIteration
result = testYield()
result.next() #啟動了這個生成器,直到第一個yield語句 hold
你去取這個 result.next(),比如print result.next(), 這個值才真正生成了,result.next()的type是int
result.send(something), 這個something會被傳到yield表達式中,m= 'something'。type(m) is str. print m才能獲得你用send傳入的值。
說明什么呢?
你用send傳什么東西,如果你在方法里並不用,就沒什么用了。我暫時是這么想的。
result.send(something)繼續尋找下一個yield,並hold
跟next()類似,send也會使得生成器停在那里。
print result.send(something) 出來的並不是something。print的是generator找到的下一個yield的值。
這翻譯的生成器,就是生成函數的返回值對吧,yield英文是啥意思?produce生產一個東西
再看一個栗子~
def h(): print 'Wen Chuan' m = yield 5 # Fighting! print 'm=', m d = yield 12 print 'We are together!' print d test = yield 35 print test c = h() current = c.next() # start generator, stop at the first yield print current current = c.send('Fighting!') # send current yield 'fighting', stop at the next yield print current c.send('got it') print list(c) print list(h())
輸出:
Wen Chuan
5
m= Fighting!
12
We are together!
got it
None
[]
Wen Chuan
m= None next() = send(None)
We are together!
None
None
[5, 12, 35]
【時隔快一年,之前學習的python忘得差不多了。於是打算重新學】
http://anandology.com/python-practice-book/iterators.html
說點正常人能看懂的。之前寫的我自己都看不懂。。下面英文是摘抄。
Generators simplifies creation of iterators. A generator is a function that produces a sequence of results instead of a single value.
Each time the yield
statement is executed the function generates a new value.
So a generator is also an iterator. You don’t have to worry about the iterator protocol.
The word “generator” is confusingly used to mean both the function that generates and what it generates. In this chapter, I’ll use the word “generator” to mean the genearted object and “generator function” to mean the function that generates it.
Can you think about how it is working internally?
When a generator function is called, it returns a generator object without even beginning execution of the function. When next
method is called for the first time, the function starts executing until it reaches yield
statement. The yielded value is returned by the next
call.
The following example demonstrates the interplay between yield
and call to next
method on generator object.
好的這段話說得不錯,我來翻譯一下。
說啊這個Generators是簡化iterator的創建的。一個Generator是用來生成一串結果的方法,而不是僅返回一個值。
每次呢,yield語句一旦執行,這個方法就生產出一個新的值。
所以呢,這個generator也是一個迭代器。你就別擔心iterator的規則它是否適用,答案是肯定的。
這個詞"generator"到底意思是它的作用是生成還是說它生成的東西呢?這一點讓人困惑。在俺們這里,我用generator來表示生成的對象,用generator function來表示它生成東西的這個作用。
當一個generator function被調用的時候呢,它會返回一個generator 對象,只是返回個對象,並不干其他的事情,甚至都沒開始執行這個方法。
當next 方法第一次被調用的時候,這個方法才開始執行,直到它遇到了一個yield語句。這個yield(量產)的值作為next的返回值。