私はhttp://www.dabeaz.com/coroutines/を見ていましたが、これは非常に興味深いと思いますが、例では理解できない動作があります。
bogus.pyの例では、ここで報告されています
# bogus.py
#
# Bogus example of a generator that produces and receives values
def countdown(n):
print "Counting down from", n
while n >= 0:
newvalue = (yield n)
# If a new value got sent in, reset n with it
if newvalue is not None:
n = newvalue
else:
n -= 1
# The holy grail countdown
c = countdown(5)
for x in c:
print x
if x == 5:
c.send(3)
send(3)
生成された数字のシーケンスは5、2、1、0n
であり、数字 3 がどこに消えたのか理解できませんyield
。 for ループに渡されません。
なぜこれが起こるのか誰かが私に説明できますか?