title: why does this works SOMETIMES in python ? (variable scope)
body: <p>This doesn't work: (global name 'a' is not defined)</p>
  
  <pre><code>if True:
    a = 3
  else:
    a = 4
  print a
  </code></pre>
  
  <p>This works:</p>
  
  <pre><code>class A:
    def f(self):
      if True:
        a = 3
      else:
        a = 4
      print a
  
  A().f()
  </code></pre>
  
  <p>This doesn't work: (global name 'a' is not defined)</p>
  
  <pre><code>class A:
    def __init__(self):
      a = 3
    def f(self):
      print a
  
  A().f()
  </code></pre>
  
  <p>Why is it working in one case and not in another ? I thought that if it works in case (2) then it is because <code>a</code> gets in the class context, but this doesn't make it work in (3)
  I saw other discussions but I don't understand everything that is said and cases vary a bit.</p>
  
  <p>EDIT:</p>
  
  <p>example 1 does NOT work, at least in a python 2.7 interpreter. This is why it examples 1 and 2 are contradictory</p>