Last active
July 16, 2020 12:09
-
-
Save kemingy/0bf6255b44440273dcda8bcae772fc8d to your computer and use it in GitHub Desktop.
global nonlocal variables
This file contains hidden or 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
x = 233 | |
def xxx(): | |
x = 222 | |
def foo(): | |
nonlocal x | |
x = 1 | |
def bar(): | |
nonlocal x | |
x = 123 | |
print(x) | |
bar() | |
print(x) | |
foo() | |
xxx() | |
print(x) |
This file contains hidden or 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
x = 233 | |
def foo(): | |
global x | |
x = 1 | |
def bar(): | |
nonlocal x # SyntaxError: no binding for nonlocal 'x' found | |
x = 123 | |
print(x) | |
bar() | |
print(x) | |
foo() | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment