python global variable, whats going on here? Explanation needed
<p>Can someone explain what's going on here</p>

<pre><code>x = 10
def foo():
    print "x in foo = ",x
    if x: x = 8    -------------&gt; mysterious line

foo()
print "x in main = ",x
</code></pre>

<p>In the above code, If i comment out the mysterious line (if x: x = 8) </p>

<p>I get the output</p>

<pre><code>x in foo =  10
x in main =  10
</code></pre>

<p>Otherwise I end up with error</p>

<blockquote>
  <p>"UnboundLocalError: local variable 'x' referenced before assignment"</p>
</blockquote>

<p>Why so?</p>

<p>I know <code>global x</code> is helpful only when I need to modify a global variable locally.</p>
