Last active
August 31, 2019 06:51
-
-
Save ksomemo/240562ceba4cc8046178d639c2a3cc15 to your computer and use it in GitHub Desktop.
global, nonlocal
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
a = 0 | |
a2 = 100 | |
def main(): | |
global a, a2 | |
print('a2', a2) | |
print(a) | |
a += 1 | |
print(a) | |
a += 5 | |
def f(): | |
a = 1 | |
b = 2 | |
def f2(): | |
nonlocal a | |
print('a', a) | |
a += 2 | |
print('a', a) | |
a += 2 | |
def f3(): | |
nonlocal a | |
print('a', a) | |
a += 2 | |
nonlocal b | |
print('b', b) | |
b += 2 | |
f3() | |
f2() | |
print('a', a) | |
print('b', b) | |
if __name__ == "__main__": | |
print(a) | |
a += 1 | |
print(a) | |
main() | |
print(a) | |
print("====") | |
f() | |
print(a) |
Author
ksomemo
commented
Aug 31, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment