DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

global vs nonlocal in Python (2)

Buy Me a Coffee

*Memo:

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() 
Enter fullscreen mode Exit fullscreen mode
""" 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() 
Enter fullscreen mode Exit fullscreen mode
""" 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() 
Enter fullscreen mode Exit fullscreen mode

<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() 
Enter fullscreen mode Exit fullscreen mode
""" 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() 
Enter fullscreen mode Exit fullscreen mode
""" 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() 
Enter fullscreen mode Exit fullscreen mode

<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 
Enter fullscreen mode Exit fullscreen mode
""" 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 
Enter fullscreen mode Exit fullscreen mode
""" 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) 
Enter fullscreen mode Exit fullscreen mode

<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 
Enter fullscreen mode Exit fullscreen mode
""" 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 
Enter fullscreen mode Exit fullscreen mode
""" 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) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)