What is the result of a yield expression in Python?
23,849
You can also send
values to generators. If no value is sent then x
is None
, otherwise x
takes on the sent value. Here is some info: http://docs.python.org/whatsnew/2.5.html#pep-342-new-generator-features
>>> def whizbang():
for i in range(10):
x = yield i
print 'got sent:', x
>>> i = whizbang()
>>> next(i)
0
>>> next(i)
got sent: None
1
>>> i.send("hi")
got sent: hi
2
Author by
slacy
Updated on February 18, 2020Comments
-
slacy about 4 years
I know that yield turns a function into a generator, but what is the return value of the yield expression itself? For example:
def whizbang(): for i in range(10): x = yield i
What is the value of variable
x
as this function executes?I've read the Python documentation: http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt and there seems to be no mention of the value of the yield expression itself.
-
slacy almost 12 yearsWow. Why this isn't mentioned in the documentation for yield is totally beyond me. Where do I submit doc bugs?
-
jamylak almost 12 years@slacy Not sure about both of those.
-
senderle almost 12 years@slacy, huh? It is mentioned in the documentation for yield.
-
senderle almost 12 years@slacy, though I admit after looking around, it's a bit hard to find from the table of contents.
-
Karl Knechtel almost 12 yearsHow would you use something like this?
-
slacy almost 12 yearsCan you link to it? I don't see it anywhere in the current docs. I did find it here: docs.python.org/release/2.5.2/ref/yieldexpr.html but not docs.python.org/release/2.7/reference/… or docs.python.org/release/2.7/reference/index.html
-
Matthew Trevor almost 12 yearsHow is this a doc bug? At docs.python.org/reference/expressions.html#yield-expressions the second paragraph ends with
The value of the yield expression after resuming depends on the method which resumed the execution.
This is followed by the method definitions withnext()
sayingWhen a generator function is resumed with a next() method, the current yield expression always evaluates to None.
The very next listed method issend()
:The value argument becomes the result of the current yield expression.
It's all there. -
slacy almost 12 years@MatthewTrevor The docs for the yield statement and yield expression are poorly cut & pasted and modified versions of each other. There's no mention at all in the yield statement docs about yield expressions (and their different behavior). I think this would be lost to many programmers, even advanced ones, like myself.
-
Matthew Trevor almost 12 yearsI'm not surprising that docs about statements don't talk about expressions, they're two separate things. As you were looking for the result of a yield expression, then reading up on statements was a waste of time, something I would expect an "advanced" programmer to be aware of. Those three lines are each very clear, tell you exactly what you wanted to know (they explicitly state what the expression evaluates to!) and were obviously placed in the documentation.
-
Anentropic over 9 yearsand interestingly you have to call
next()
at least once beforesend()
or you getTypeError: can't send non-None value to a just-started generator