title: How do Python generator functions maintain local state?
body: <p>According to the docs at <a href="https://docs.python.org/2/reference/simple_stmts.html#yield" rel="nofollow">https://docs.python.org/2/reference/simple_stmts.html#yield</a>, </p>
  
  <blockquote>
  all local state is retained, including the current bindings of local variables, the instruction pointer, and the internal evaluation stack: enough information is saved so that the next time next() is invoked, the function can proceed exactly as if the yield statement were just another external call.</blockquote>
  
  <p>Here's a simple case:</p>
  
  <pre><code>def generator():
      my_list = range(10)
      print "my_list got assigned"
      for i in my_list:
          print i
          yield
      return
  </code></pre>
  
  <p>In the shell, generator() behaves like this:</p>
  
  <pre><code>&gt;&gt;&gt;&gt;generator().next()
  my_list got assigned
  0
  &gt;&gt;&gt;&gt;generator().next()
  my_list got assigned
  0
  </code></pre>
  
  <p>I would have thought that my_list would not get reassigned each time .next() is called. Can someone explain why this happens, and why it seems like the docs contradict this?</p>