While 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 deduces:
Start from 5 Yielding 5 5 Yielding 3 Yielding 2 2 Yielding 1 1 Yielding 0 0
In particular, it skips printing 3 . Why?
This question does not answer this question because I am not asking what send does. It returns the values back to the function. I ask why, after the release of send(3) , the next result, which should give 3, does not cause the for 3 loop to print.
source share