问题
I was looking at http://www.dabeaz.com/coroutines/, which I am finding very interesting, but in an example there is a behavior I do not understand.
In the bogus.py example, reported here
# 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)
The sequence of numbers generated is 5, 2, 1, 0, and I can not understand where the number 3 is gone: after the send(3), the variable n is correctly set, but at the second execution of yield, it looks like the value 3 is just non yielded to the for loop.
Can someone clarify me why this happen?
回答1:
The 3 was returned from .send(), but discarded. The generator produces 5, 3, 2, 1, 0; but because the 3 is returned to the .send() call you don't see that value printed. The for loop never gets to see it.
What happens is this:
- first time the
forloop callsnext()on the generator, the code advances until5is yielded. x == 5isTrue, soc.send(3)is called. The code advances through the generator function, andnewvalueis set to3.- The generator does not pause there, it now has control. The generator runs through the
whileloop and comes back to the(yield n)expression.3is yielded. It becomes the return value forc.send(3). The return value is discarded here. - The
forloop continues, callsnext()again. The generator is continued again withyieldreturningNone, loops round ton -= 1and yielding2. - The
forloop continues to callnext()on the generator,1and0are yielded, the generator ends.
Qouting from the generator.send() documentation:
Resumes the execution and “sends” a value into the generator function. The
valueargument becomes the result of the currentyieldexpression. Thesend()method returns the next value yielded by the generator, or raisesStopIterationif the generator exits without yielding another value.
Emphasis mine.
来源:https://stackoverflow.com/questions/17990624/value-get-lost-in-python-generator-coroutine