我與發電機及發電機表達玩弄,我不能完全肯定,我瞭解他們的工作(some reference material):試圖瞭解產量爲表達
>>> a = (x for x in range(10))
>>> next(a)
0
>>> next(a)
1
>>> a.send(-1)
2
>>> next(a)
3
所以看起來generator.send
是忽略。這是有道理的(我猜),因爲沒有明確的yield
表達趕發送信息......
然而,
>>> a = ((yield x) for x in range(10))
>>> next(a)
0
>>> print next(a)
None
>>> print next(a)
1
>>> print next(a)
None
>>> a.send(-1) #this send is ignored, Why? ... there's a yield to catch it...
2
>>> print next(a)
None
>>> print next(a)
3
>>> a.send(-1) #this send isn't ignored
-1
我明白這是相當遠在那裏,我(現在)能「T認爲用例這個的(所以不要問;)
我大多隻是探索,試圖找出這些不同的發電機工作方法(以及如何在一般的發電機表達式工作)如何。爲什麼我的第二個例子在產生一個合理的價值和None
之間交替?另外,任何人都可以解釋爲什麼我的一個generator.send
的被忽略,而另一個不是?
檢查此鏈接是否可以幫助您... http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained –