title: Why nested functions can access variables from outer functions, but are not allowed to modify them
body: <p>In the 2nd case below, Python tries to look for a local variable. When it doesn't find one, why can't it look in the outer scope like it does for the 1st case? </p>
  
  <p>This looks for x in the local scope, then outer scope:</p>
  
  <pre><code>def f1():
      x = 5
      def f2():
           print x
  </code></pre>
  
  <p>This gives <code>local variable 'x' referenced before assignment</code> error:</p>
  
  <pre><code>def f1():
      x = 5
      def f2():
          x+=1
  </code></pre>
  
  <p>I am not allowed to modify the signature of function f2() so I can not pass and return values of x. However, I do need a way to modify x. Is there a way to explicitly tell Python to look for a variable name in the outer scope (something similar to the <code>global</code> keyword)?</p>
  
  <p>Python version: 2.7</p>