Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Created January 12, 2023 11:08
Show Gist options
  • Save mdpabel/2c4d8d87a7d343e7857c6c03226e9188 to your computer and use it in GitHub Desktop.
Save mdpabel/2c4d8d87a7d343e7857c6c03226e9188 to your computer and use it in GitHub Desktop.
Python - Global, Local and Nonlocal Variables
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