Possible Duplicate:
Python variable scope question
The Python Manual defines the scope as:
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.
I have this program:
import random
def f():
a = "Is the scope static?"
if random.randint(0, 1) == 1:
del a
print a
There is a 50% chance that the print will fail:
>>> f()
Is the scope static?
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in f
UnboundLocalError: local variable 'a' referenced before assignment
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"?