local/global variable confusion in python
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/855493/referenced-before-assignment-error-in-python">referenced before assignment error in python</a>  </p>
</blockquote>



<p>I'm getting a strange error in python. The following ipython log sums it up:</p>

<pre><code>In [10]: def confused(stuff):
   ....:     print huh
   ....:     return stuff
   ....: 

In [11]: confused(87)
0
Out[11]: 87

In [12]: def confused(stuff):
   ....:     print huh
   ....:     huh += 1
   ....:     return stuff
   ....: 

In [13]: confused(9)
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
/home/max/verk/btr-email/build/x86_64/bin/ipython in &lt;module&gt;()
----&gt; 1 confused(9)

/home/max/verk/btr-email/build/x86_64/bin/ipython in confused(stuff)
      1 def confused(stuff):
----&gt; 2     print huh
      3     huh += 1
      4     return stuff

UnboundLocalError: local variable 'huh' referenced before assignment
</code></pre>

<p>The only difference between the function that works and the one that throws an error is the +=1 line, and even then, it throws an error on a line which was previously working! It also doesn't throw an error if I put <code>global huh</code> before referencing <code>huh</code> in the 2nd version of the method.</p>

<p>Why does adding a line where I add one to the variable suddenly change it from a global to a local variable?</p>
