4 ms·
Python doesn't have block scope, but most people don't seem to notice: def foo(): if False: x = 123 print("x is visible out he
by xscott 4y ago
Python doesn't have block scope, but most people don't seem to notice:
def foo():
if False:
x = 123
print("x is visible out here:", x)
That will generate an error because `x` is "referenced before assignment", but the variable exists for the entire function. This can bite you in subtle ways:
x = "is global out here"
def foo():
print("all good:", x)
def bar():
print("not good:", x)
if False:
x = "now x is local, even before this declaration"