Created
August 9, 2012 18:28
-
-
Save keturn/3306840 to your computer and use it in GitHub Desktop.
python shadowing variables in inner scopes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(a): | |
print "foo a", a | |
def bar(x): | |
print "bar reading a", a | |
def baz(x): | |
a = x | |
print "baz wrote to a", a | |
def quux(x): | |
a += x | |
print "a is quux'd", a | |
return bar, baz, quux | |
ar, az, uux = foo(100) | |
ar(1) # works | |
az(2) # also passes | |
uux(4) # UnboundLocalError: local variable 'a' referenced before assignment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment