title: When to use return (yield something)?
body: <p>For a long time I didn't know you can't put <code>return</code> in front of a yield statement. But actually you can:</p>
  
  <pre><code>def gen():
      return (yield 42)
  </code></pre>
  
  <p>which is similar to</p>
  
  <pre><code>def gen():
      yield 42
      return
  </code></pre>
  
  <p>And the only usage I can think of is to attach sent value to <code>StopIteration</code>: <a href="https://www.python.org/dev/peps/pep-0380/#proposal" rel="noreferrer">pep-0380</a></p>
  
  <blockquote>
    <p>return expr in a generator causes StopIteration(expr) to be raised
    upon exit from the generator.</p>
  </blockquote>
  
  <pre><code>def gen():
      return (yield 42)
  
  g = gen()
  print(next(g))  # 42
  try:
      g.send('AAAA')
  except StopIteration as e:
      print(e.value)  # 'AAAA'
  </code></pre>
  
  <p>But this can be done using an extra variable too, which is more explicit:</p>
  
  <pre><code>def gen():
      a = yield 42
      return a
  
  g = gen()
  print(next(g))
  try:
      g.send('AAAA')
  except StopIteration as e:
      print(e.value)  # 'AAAA'
  </code></pre>
  
  <p>So it seems <code>return (yield xxx)</code> is merely a syntactic sugar. Am I missing something?</p>