title: python global variable doesn't work in function when it is evaluated by an if statement in function
body: <p>Example 1:</p>
  
  <pre><code>a=1
  def b():
      print a
      a=2
      print a
  </code></pre>
  
  <p>Example 2:</p>
  
  <pre><code>a=1
  def b():
      print a
      if a==1:
          a=2
      print a
  </code></pre>
  
  <p>Example 1 works as expected, Example 2 fails with
  UnboundLocalError: local variable 'a' referenced before assignment
  on the first print a</p>
  
  <p>Can someone explain why this happens ? Is it an error or a feature ?</p>
  
  <p>The second example is not very useful but I don't see why it shouldn't work.
  I would expect function b to first print the global a then check if global a is 1. If that's true local a would be set to 2. Then either global a would be printed or local a depending on the value of global a. Well in the example global a is 1, so I would expect to see local a printed with the value of 2.</p>
  
  <p>Example 3:</p>
  
  <pre><code>a=1
  def b():
      print a
  </code></pre>
  
  <p>works</p>
  
  <p>Example 4:</p>
  
  <pre><code>a=1
  def b():
      print a
      a=2
  </code></pre>
  
  <p>fails, as does Example 1 as correctly commented, I had actually only tested Example 3 and thought it was the same as Example 1.</p>
  
  <p>Now I understand the ever so often repeated thing about the whole scope. It's completely new to me and I have fun with the following example (Ipython):</p>
  
  <pre><code>In [13]: a=1
  
  In [14]: def b():
     ....:     a=2
     ....:     global a
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
     ....:     print a
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
     ....:     
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;input&gt;:3: SyntaxWarning: name 'a' is assigned to before global declaration
  &lt;ipython-input-14-4fe1c6cdc9d0&gt;:3: SyntaxWarning: name 'a' is assigned to before global     
  declaration
    global a
  
  In [15]: b()
  2
  
  In [16]: print a
  2
  </code></pre>
  
  <p>So the mechanism differs from declaring a function or things like that where I can only access the state after I have changed it.</p>
  
  <p>Are there any other actions in python that work like this - travelling through time ?</p>
  
  <p>To make the examples work globals() could be used:</p>
  
  <p>Example 1 working:</p>
  
  <pre><code>a=1
  def b():
      print globals()["a"]
      a=2
      print a
  </code></pre>
  
  <p>Example 2 working:</p>
  
  <pre><code>a=1
  def b():
      global_a=globals()["a"]
      print global_a
      if global_a==1:
          a=2
      print a
  </code></pre>
  
  <p>Example 4 working:</p>
  
  <pre><code>a=1
  def b():
      print globals()["a"]
      a=2
  </code></pre>