title: Nested function variable scoping
body: <h1>Failing Case</h1>
  <p>I ran across an <code>UnboundLocalError</code> exception that I could not understand.</p>
  <p>I isolated the simplest chunk of the code:</p>
  <pre class="lang-py prettyprint-override"><code>def wrapper_func():
     x = 0
     def test_unbound():
         x += 1
         return x
     return test_unbound()
  </code></pre>
  <p>Then if I call it:</p>
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; wrapper_func()
  Traceback (most recent call last):
    File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;&lt;stdin&gt;&quot;, line 6, in wrapper_func
    File &quot;&lt;stdin&gt;&quot;, line 4, in test_unbound
  UnboundLocalError: local variable 'x' referenced before assignment
  </code></pre>
  <p>But as I understood it, <code>x</code> should be available to the inner function.</p>
  <h1>Functioning Case</h1>
  <p>I tested it by doing something <em>almost</em> identical:</p>
  <pre class="lang-py prettyprint-override"><code>def outer_func():
     x = 1
     def inner_func(val):
        print(val + x)
     inner_func(2)
  </code></pre>
  <p>If I test this out, no error:</p>
  <pre class="lang-py prettyprint-override"><code>outer_func()
  3
  </code></pre>
  <h1>What Am I Missing?</h1>
  <p>Can someone help me understand why the failing case fails and the successful case works?</p>