Last active
August 29, 2015 13:58
-
-
Save jminuscula/10024966 to your computer and use it in GitHub Desktop.
Python 3 - Name lookup example
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(): | |
x = 1 | |
def bar(): | |
x = 2 | |
print(x) | |
bar() | |
foo() |
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
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() |
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(): | |
x = 1 | |
def bar(): | |
nonlocal x | |
x += 1 | |
print(x) | |
bar() | |
foo() |
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(x=[]): | |
x.append(1) | |
print(x) | |
foo() | |
foo() | |
foo() |
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(): | |
x = 1 | |
def bar(): | |
print(x + 1) | |
bar() | |
foo() |
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(): | |
x = 1 | |
def bar(): | |
x += 1 | |
print(x) | |
bar() | |
foo() |
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(): | |
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