71

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.

Mischa Arefiev
  • 5,227
  • 4
  • 26
  • 34
slacy
  • 11,397
  • 8
  • 56
  • 61
  • Of course, the experimental result is that it always returns "None" but I just want to confirm this. – slacy May 22 '12 at 03:45
  • 2
    Your link points to the documentation of `yield` as a statement, but you are using a [`yield` expression](http://docs.python.org/reference/expressions.html#yield-expressions) in the example code. – Sven Marnach May 22 '12 at 10:09
  • FWIW, `yield` expressions were introduced in 2.5 following the approval of [PEP 342](https://peps.python.org/pep-0342/) – mbdeaton Apr 11 '23 at 23:56

1 Answers1

81

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
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • 6
    Wow. Why this isn't mentioned in the documentation for yield is totally beyond me. Where do I submit doc bugs? – slacy May 22 '12 at 03:53
  • @slacy Not sure about both of those. – jamylak May 22 '12 at 03:54
  • 5
    @slacy, huh? It _is_ mentioned in the documentation for [yield](http://docs.python.org/reference/expressions.html#generator.send). – senderle May 22 '12 at 04:08
  • 4
    @slacy, though I admit after looking around, it's a bit hard to find from the table of contents. – senderle May 22 '12 at 04:19
  • How would you use something like this? – Karl Knechtel May 22 '12 at 04:47
  • Can you link to it? I don't see it anywhere in the current docs. I did find it here: http://docs.python.org/release/2.5.2/ref/yieldexpr.html but not http://docs.python.org/release/2.7/reference/simple_stmts.html#grammar-token-yield_stmt or http://docs.python.org/release/2.7/reference/index.html – slacy May 22 '12 at 04:47
  • 8
    How is this a doc bug? At http://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. – Matthew Trevor May 22 '12 at 06:49
  • 1
    @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. – slacy May 22 '12 at 16:37
  • 2
    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. – Matthew Trevor May 23 '12 at 01:31
  • 2
    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` – Anentropic Aug 27 '14 at 15:44