title: Why can't I set a global variable in Python?
body: <p>How do global variables work in Python?  I know global variables are evil, I'm just experimenting.</p>
  
  <p>This does not work in python:</p>
  
  <pre><code>G = None
  
  def foo():
      if G is None:
          G = 1
  
  foo()
  </code></pre>
  
  <p>I get an error:</p>
  
  <pre><code>UnboundLocalError: local variable 'G' referenced before assignment
  </code></pre>
  
  <p>What am I doing wrong?</p>