*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() print(num) # 3 first() print(num) # 2
""" 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() print(num) # 13 first() print(num) # 2
""" 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() print(num) first() print(num)
<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() print(num) # 3 num = 2 # <- ✖ first() print(num) # 2
""" 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() print(num) # 13 num = 2 # <- ✖ first() print(num) # 2
""" 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() print(num) num = 2 # <- ✖ first() print(num)
Top comments (0)