Value Get Lost In Python Generator/coroutine
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, reporte
Solution 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
for
loop callsnext()
on the generator, the code advances until5
is yielded. x == 5
isTrue
, soc.send(3)
is called. The code advances through the generator function, andnewvalue
is set to3
.- The generator does not pause there, it now has control. The generator runs through the
while
loop and comes back to the(yield n)
expression.3
is yielded. It becomes the return value forc.send(3)
. The return value is discarded here. - The
for
loop continues, callsnext()
again. The generator is continued again withyield
returningNone
, loops round ton -= 1
and yielding2
. - The
for
loop continues to callnext()
on the generator,1
and0
are yielded, the generator ends.
Qouting from the generator.send()
documentation:
Resumes the execution and “sends” a value into the generator function. The
value
argument becomes the result of the currentyield
expression. Thesend()
method returns the next value yielded by the generator, or raisesStopIteration
if the generator exits without yielding another value.
Emphasis mine.
Post a Comment for "Value Get Lost In Python Generator/coroutine"