title: Exact return point of yield in python generators
body: <p>In a quest to teach myself Python generators, especially their <code>send()</code> method, I wanted to write a toy example that would allow me calculate an "interruptible" Fibonacci sequence.  </p>
  
  <p>Eventually I succeeded, but I don't understand exactly what is going on.  Consider these two examples.</p>
  
  <h3>Successful example:</h3>
  
  <pre><code>def genFib(a=0, b=1):
      while True:
          c = yield a+b
          if c:
              a, b = b, c
          else:
              a, b = b, a+b
      return
  ​
  fib_number = genFib()
  ​
  print fib_number.next()
  print fib_number.next()
  print fib_number.next()
  print fib_number.next()
  print fib_number.next()
  print fib_number.send(100)
  print fib_number.next()
  print fib_number.next()
  ​
  1
  2
  3
  5
  8
  105
  205
  310
  </code></pre>
  
  <h3>Failure that I thought would work:</h3>
  
  <pre><code>def genFib(a=0, b=1):
      while True:
          c = yield a+b
          a, b = b, c
      return
  ​
  fib_number = genFib()
  ​
  print fib_number.next()
  print fib_number.next()
  1
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-59-4513153ea517&gt; in &lt;module&gt;()
        8 
        9 print fib_number.next()
  ---&gt; 10 print fib_number.next()
  
  &lt;ipython-input-59-4513153ea517&gt; in genFib(a, b)
        1 def genFib(a=0, b=1):
        2     while True:
  ----&gt; 3         c = yield a+b
        4         a, b = b, c
        5     return
  
  TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
  </code></pre>
  
  <p><strong>Why does the second example not work?</strong> In particular, the second example seems assign a value of <code>None</code> to <code>c</code> in all <em>but the first</em> executions of the generator.  Why?  How does it know to assign to <code>c</code> the first time <code>next()</code> is called, but not the next <code>next()</code> time?</p>
  
  <p>My understanding was that generators resume execution (when they are called again) beginning on the line <em>after</em> the yield line, but my example has me thinking this is wrong, at least in some cases.  Where exactly does execution resume from?  Does it re-execute the <code>yield</code> line but this time with somehow leaving off everything after the <code>yield</code>?  How does <code>c</code> get assigned to <code>None</code>?</p>