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
Share:
23,849
slacy
Author by

slacy

Updated on February 18, 2020

Comments

  • slacy
    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
    slacy almost 12 years
    Wow. Why this isn't mentioned in the documentation for yield is totally beyond me. Where do I submit doc bugs?
  • jamylak
    jamylak almost 12 years
    @slacy Not sure about both of those.
  • senderle
    senderle almost 12 years
    @slacy, huh? It is mentioned in the documentation for yield.
  • senderle
    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
    Karl Knechtel almost 12 years
    How would you use something like this?
  • slacy
    slacy almost 12 years
    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.html
  • Matthew Trevor
    Matthew Trevor almost 12 years
    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.
  • slacy
    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
    Matthew Trevor almost 12 years
    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.
  • Anentropic
    Anentropic over 9 years
    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 generator