title: confused by python3 closure
body: <p>this code </p>
  
  <pre><code>def gen(x):
      def f():
          return x
  
      return f
  
  print(gen(1)())
  </code></pre>
  
  <p>works well.</p>
  
  <p>but when i run this code,</p>
  
  <pre><code>def gen(x):
      def f():
          x += 1
          return x
  
      return f
  
  print(gen(1)())
  </code></pre>
  
  <p>I got an error shows that</p>
  
  <blockquote>
    <p>UnboundLocalError: local variable 'x' referenced before assignment</p>
  </blockquote>
  
  <p>what happened to it?
  and How to understand the closure of python3 .</p>