Skip to content

Instantly share code, notes, and snippets.

@keturn
Created August 9, 2012 18:28
Show Gist options
  • Save keturn/3306840 to your computer and use it in GitHub Desktop.
Save keturn/3306840 to your computer and use it in GitHub Desktop.
python shadowing variables in inner scopes
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