Created
January 12, 2023 11:08
-
-
Save mdpabel/2c4d8d87a7d343e7857c6c03226e9188 to your computer and use it in GitHub Desktop.
Python - Global, Local and Nonlocal Variables
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
v = 0 # global variable | |
def a(): | |
def test(n): | |
nonlocal m # used in nested function whose local scope is not defined | |
global v # outside of function or in global scope | |
if n == 5: | |
return n | |
t = test(n + 1) # local variable | |
m += t + n | |
v += t + n | |
return n + t | |
m = 0 | |
test(1) | |
print(m) | |
a() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment