Why can't I set a global variable in Python?
<p>I'm trying to understand how global variables work in Python. (Please, no lectures about how global variables are evil; I'm just experimenting). Anyway, this doesn't work:</p>

<pre><code>G = None

def foo():
    if G is None:
        G = 1

foo()
</code></pre>

<p>I get:</p>

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

<p>What am I doing wrong?</p>
