>>56,57
My point is: that's how Python scopes work. The bytecode compiler statically identifies every name that belongs to a scope. Any expression that uses the scope has access to every variable of the scope, no matter where it is first assigned or used, lexically.
It is not a good thing in the whole scheme of things, I personally prefer explicit declaration with additional safeguards a la C#.
However, it's the best one can have
given the "assignment is declaration" rule. Accounting for relative positions of assignment in the scope would produce subtle errors with horrible consequences.
Also, how is the following code supposed to work? Is correct scope determined statically or dynamically?
x = 10
def f(n):
if n: x = 20
print x