Reading about Python coroutines, I came across this code:
def countdown(n):
print("Start from {}".format(n))
while n >= 0:
print("Yielding {}".format(n))
newv = yield n
if newv is not None:
n = newv
else:
n -= 1
c = countdown(5)
for n in c:
print(n)
if n == 5: c.send(2)
which curiously outputs:
Start from 5
Yielding 5
5
Yielding 3
Yielding 2
2
Yielding 1
1
Yielding 0
0
In particular, it misses printing the 3
. Why?
The referenced question does not answer this question because I am not asking what send
does. It sends values back into the function. What I am asking is why, after I issue send(3)
, does the next yield, which should yield 3, not cause the for loop to print 3.