25

For a long time I didn't know you can't put return in front of a yield statement. But actually you can:

def gen():
    return (yield 42)

which is similar to

def gen():
    yield 42
    return

And the only usage I can think of is to attach sent value to StopIteration: pep-0380

return expr in a generator causes StopIteration(expr) to be raised upon exit from the generator.

def gen():
    return (yield 42)

g = gen()
print(next(g))  # 42
try:
    g.send('AAAA')
except StopIteration as e:
    print(e.value)  # 'AAAA'

But this can be done using an extra variable too, which is more explicit:

def gen():
    a = yield 42
    return a

g = gen()
print(next(g))
try:
    g.send('AAAA')
except StopIteration as e:
    print(e.value)  # 'AAAA'

So it seems return (yield xxx) is merely a syntactic sugar. Am I missing something?

laike9m
  • 18,344
  • 20
  • 107
  • 140
  • 3
    "But this can be done using an extra variable too, which is more explicit" - you could say that about any `return` statement. `return x + y` becomes `z = x + y; return z`. `return foo()` becomes `x = foo(); return x`. There's nothing specific to `return (yield whatever)` here. – user2357112 Feb 12 '17 at 05:44
  • As for `StopIteration` arguments, you're not expected to access those explicitly; those are just how `yield from` expression values happen to be implemented. It's a published part of the API, but usually not an interesting part. – user2357112 Feb 12 '17 at 05:46

1 Answers1

9

Inside a generator the expressions (yield 42) will yield the value 42, but it also returns a value which is either None, if you use next(generator) or a given value if you use generator.send(value).

So as you say, you could use an intermediate value to get the same behavior, not because this is syntactical sugar, but because the yield expressions is literally returning the value you send it.

You could equally do something like

def my_generator():
    return (yield (yield 42) + 10)

If we call this, using the sequence of calls:

g = my_generator()
print(next(g))
try:
    print('first response:', g.send(1))
    print('Second response:', g.send(22))
    print('third response:', g.send(3))
except StopIteration as e:
    print('stopped at', e.value)

First we get the output of 42, and the generator is essentially paused in a state you could describe like: return (yield <Input will go here> + 10), If we then call g.send(1) we get the output 11. and the generator is now in the state: return <Input will go here>, then sending g.send(22) will throw a StopIteration(22), because of the way return is handled in generators. So you never get to the third send because of the exception.

I hope this example makes it a bit more apparent how yield works in generators and why the syntax return (yield something) is nothing special or exotic and works exactly how you'd expect it.

As for the literal question, when would you do this? Well when ever you want to yield something, and then later return a StopIteration echoing the input of the user sent to the generator. Because this is literally what the code is stating. I expect that such behavior is very rarely wanted.

wim
  • 338,267
  • 99
  • 616
  • 750
jVincent
  • 323
  • 2
  • 3
  • Interesting. If I do something like `x = (yield 42)` and then `yield x + 10`, use `g.send`, it seems `x` becomes `None`, is that expected? Why doesn't `x` become what I've passed to `send`? – juanpa.arrivillaga Apr 19 '17 at 19:07
  • @juanpa.arrivillaga I'm sorry but you'll have to post some code showing what you are doing. piecing together what you are writing in the correct way gives the expected results, so likely you are not putting it together correctly since it's not working for you. – jVincent May 09 '17 at 07:45