title: Python closure function losing outer variable access
body: <p>I just learned python @ decorator, it's cool, but soon I found my modified code coming out weird problems.</p>
  
  <pre><code>def with_wrapper(param1):
      def dummy_wrapper(fn):
          print param1
          param1 = 'new'
          fn(param1)
      return dummy_wrapper
  
  def dummy():
      @with_wrapper('param1')
      def implementation(param2):
          print param2
  
  dummy()
  </code></pre>
  
  <p>I debug it, it throws out exception at print param1</p>
  
  <pre><code>UnboundLocalError: local variable 'param1' referenced before assignment
  </code></pre>
  
  <p>If I remove  <code>param1 = 'new'</code> this line, without any modify operation (link to new object) on variables from outer scope, this routine might working.</p>
  
  <p>Is it meaning I only have made one copy of outer scope variables, then make modification?</p>
  
  <hr>
  
  <p>Thanks Delnan, it's essential to closure.
  Likely answer from here:
  <a href="https://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to-language-x-closures">What limitations have closures in Python compared to language X closures?</a></p>
  
  <p>Similar code as:</p>
  
  <pre><code>def e(a):
      def f():
          print a
          a = '1'
      f()
  e('2')
  </code></pre>
  
  <p>And also this seems previous annoying global variable:</p>
  
  <pre><code>a = '1'
  def b():
      #global a
      print a
      a = '2'
  b()
  </code></pre>
  
  <p>This is fixed by add <em>global</em> symbol.
  But for closure, no such symbol found.
  Thanks unutbu, Python 3 gave us <em>nonlocal</em>.</p>
  
  <p>I know from above directly accessing to outer variable is read-only.
  but it's kind of uncomfortable to see preceded reading variable(print var) is also affected.</p>