Skip to content

Instantly share code, notes, and snippets.

@jminuscula
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jminuscula/10024966 to your computer and use it in GitHub Desktop.
Save jminuscula/10024966 to your computer and use it in GitHub Desktop.
Python 3 - Name lookup example
def foo():
x = 1
def bar():
x = 2
print(x)
bar()
foo()
a_global = 1
def foo():
a_local = 2
a_nonlocal = 3
def bar():
a_local = 4
print(a_local, "is local to bar")
print(a_nonlocal, "is in the scope of the enclosing function foo")
bar()
print(a_global, "is in the global namespace for this module")
print(int, "is a builtin name")
foo()
def foo():
x = 1
def bar():
nonlocal x
x += 1
print(x)
bar()
foo()
def foo(x=[]):
x.append(1)
print(x)
foo()
foo()
foo()
def foo():
x = 1
def bar():
print(x + 1)
bar()
foo()
def foo():
x = 1
def bar():
x += 1
print(x)
bar()
foo()
def foo():
x = 1
def bar():
print(x)
if False:
x = 2
bar()
foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment