Reputation: 6561
This snippet of python I wrote is not behaving the way I expect, given my understanding of yield
. Perhaps someone could enlighten me.
def printvar():
yield 3
data = yield
print(data)
data = yield
print(data)
x = printvar()
print(next(x))
x.send(4)
x.send(5)
What I expect:
3
4
5
What I get:
3
5
Can anyone explain this behavior?
Upvotes: 2
Views: 233
Reputation: 251488
There are two issues. One is that by advancing the generator three times, you only advance it up to the last yield, not to the code after that. The other is that send
puts its value in as the value of the yield statement where execution was previously suspended, not at the next one. Here is a step-by-step explanation of what happens:
next
executes the first yield 3
.send(4)
causes yield 3
to evaluate to 4, but you don't do anything with this in printvar
, so the value is discarded. .send(4)
also advances the generator to the first data = yield
, yielding None..send(5)
causes this first data = yield
to set data to 5, and also advances the generator to the last yield, again yielding None.yield
, never executing the last print
.The thing to remember when using send
is that it sends the value to the place that already yielded, not to the place where it's going to yield next. See this similar question for more explanation.
Upvotes: 7
Reputation: 598
Try this:
def printvar():
data = yield 3
print(data)
data = yield
print(data)
x = printvar()
print(next(x))
x.send(4)
x.send(5)
In your orginal code the send(4) goes nowhere because the yield statement is not collecting anything.
Upvotes: 3