*Memo:
- My post explains global and nonlocal with 3 functions (1).
- My post explains global and nonlocal with 3 functions (3).
A nonlocal statement can refer to a non-local variable as shown below:
<Read(Intuitive version)>:
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
num = 3 # <- ✖
def second():
num = 4 # <- 〇
def third():
nonlocal num # Here
print(num) # 4
third()
second()
first()
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
num = 3 # <- 〇
def second():
# num = 4 # <- Commented
def third():
nonlocal num # Here
print(num) # 3
third()
second()
first()
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
# num = 3 # <- Commented
def second():
# num = 4 # <- Commented
def third():
nonlocal num # SyntaxError: no binding
print(num) # for nonlocal 'num' found
third()
second()
first()
<Read(Unintuitive version)>:
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # Here
print(num) # 4
num = 4 # <- 〇
third()
num = 3 # <- ✖
second()
num = 2 # <- ✖
first()
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # Here
print(num) # 3
# num = 4 # <- Commented
third()
num = 3 # <- 〇
second()
num = 2 # <- ✖
first()
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # SyntaxError: no binding
print(num) # for nonlocal 'num' found
# num = 4 # <- Commented
third()
# num = 3 # <- Commented
second()
num = 2 # <- ✖
first()
<Change(Intuitive version)>:
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
num = 3 # <- ✖
def second():
num = 4 # <- 〇
def third():
nonlocal num # Here
num += 10 # Here
print(num) # 14
third()
print(num) # 14
second()
first()
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
num = 3 # <- 〇
def second():
# num = 4 # <- Commented
def third():
nonlocal num # Here
num += 10 # Here
print(num) # 13
third()
print(num) # 13
second()
first()
""" It's from the viewpoint of `third()` """
num = 2 # <- ✖
def first():
# num = 3 # <- Commented
def second():
# num = 4 # <- Commented
def third():
nonlocal num # SyntaxError: no binding
num += 10 # for nonlocal 'num' found
print(num)
third()
print(num)
second()
first()
<Change(Unintuitive version)>:
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # Here
num += 10 # Here
print(num) # 14
num = 4 # <- 〇
third()
print(num) # 14
num = 3 # <- ✖
second()
num = 2 # <- ✖
first()
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # Here
num += 10 # Here
print(num) # 13
# num = 4 # <- Commented
third()
print(num) # 13
num = 3 # <- 〇
second()
num = 2 # <- ✖
first()
""" It's from the viewpoint of `third()` """
def first():
def second():
def third():
nonlocal num # SyntaxError: no binding
num += 10 # for nonlocal 'num' found
print(num)
# num = 4 # <- Commented
third()
print(num)
# num = 3 # <- Commented
second()
num = 2 # <- ✖
first()
Top comments (0)