title: What is the scope of the variable "a"?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/370357/python-variable-scope-question">Python variable scope question</a>  </p>
  </blockquote>
  
  
  
  <p>The Python Manual defines the scope as:</p>
  
  <blockquote>
    <p>A scope defines the visibility of a name within a block. If a local
    variable is defined in a block, its scope includes that block. If the
    definition occurs in a function block, the scope extends to any blocks
    contained within the defining one, unless a contained block introduces
    a different binding for the name.</p>
  </blockquote>
  
  <p>I have this program:</p>
  
  <pre><code>import random
  def f():
    a = "Is the scope static?"
    if random.randint(0, 1) == 1:
      del a
    print a
  </code></pre>
  
  <p>There is a 50% chance that the print will fail:</p>
  
  <pre><code>&gt;&gt;&gt; f()
  Is the scope static?
  &gt;&gt;&gt; f()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 5, in f
  UnboundLocalError: local variable 'a' referenced before assignment
  </code></pre>
  
  <p>By that, I think that there is a 50% chance that the print statement is outside the scope of 'a', but I can be wrong. What is the "correct" interpretation of scope in Python? Is the scope of a variable in Python defined statically? What is the scope of the variable "a"?</p>