What is the result of a yield expression in Python?
Asked Answered
A

1

76

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.

Augur answered 22/5, 2012 at 3:44 Comment(3)
Of course, the experimental result is that it always returns "None" but I just want to confirm this.Augur
Your link points to the documentation of yield as a statement, but you are using a yield expression in the example code.Thursday
FWIW, yield expressions were introduced in 2.5 following the approval of PEP 342Lever
E
84

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
Elna answered 22/5, 2012 at 3:49 Comment(10)
Wow. Why this isn't mentioned in the documentation for yield is totally beyond me. Where do I submit doc bugs?Augur
@Augur Not sure about both of those.Elna
@slacy, huh? It is mentioned in the documentation for yield.Grime
@slacy, though I admit after looking around, it's a bit hard to find from the table of contents.Grime
How would you use something like this?Deaden
Can 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.htmlAugur
How 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 with next() saying When a generator function is resumed with a next() method, the current yield expression always evaluates to None. The very next listed method is send(): The value argument becomes the result of the current yield expression. It's all there.Photocompose
@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.Augur
I'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.Photocompose
and interestingly you have to call next() at least once before send() or you get TypeError: can't send non-None value to a just-started generatorChit

© 2022 - 2024 — McMap. All rights reserved.