DEV Community

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

Posted on • Edited on

global vs nonlocal in Python (7)

Buy Me a Coffee

*Memo:

With 3 classes and 3 functions, there are 4 kinds of variables from the viewpoint of third() as shown below:

  • A global variable is the variable out of any functions and classes.
  • A non-local variable is the variable within outer functions.
  • A local variable is the variable which is within its function.
  • A class variable is the variable within its class.
  • A global and non-local variable can be accessed from a function as long as the function is called and run after them even if the function is defined before them as show in unintuitive version.

<Intuitive version>:

""" It's from the viewpoint of `third()` """ num = 2 # <- Global variable print(num) # 2 class Cls1: num = 3 # <- Class variable  print(num) # 3  def first(self): num = 4 # <- Non-local variable  print(num) # 4  class Cls2: num = 5 # <- Class variable  print(num) # 5  def second(self): num = 6 # <- Non-local variable  print(num) # 6  class Cls3: num = 7 # <- Class variable  print(num) # 7  def third(self): num = 8 # <- Local variable  print(num) # 8  Cls3().third() Cls2().second() Cls1().first() 
Enter fullscreen mode Exit fullscreen mode

<Unintuitive version>:

""" It's from the viewpoint of `third()` """ class Cls1: def first(self): class Cls2: def second(self): class Cls3: def third(self): num = 8 # <- Local variable  print(num) # 8  num = 7 # <- Class variable  print(num) # 7  num = 6 # <- Non-local variable  print(num) # 6  Cls3().third() num = 5 # <- Class variable  print(num) # 5  num = 4 # <- Non-local variable  print(num) # 4  Cls2().second() num = 3 # <- Class variable  print(num) # 3 num = 2 # <- Global variable print(num) # 2 Cls1().first() 
Enter fullscreen mode Exit fullscreen mode

A global statement can refer to a global variable as shown below:

*Memo:

  • The doc explains the rules for local and global variables in Python.

<Read(Intuitive version)>:

""" It's from the viewpoint of `third()` """ num = 2 # <- 〇 class Cls1: num = 3 # <- ✖  def first(self): num = 4 # <- ✖  class Cls2: num = 5 # <- ✖  def second(self): num = 6 # <- ✖  class Cls3: num = 7 # <- ✖  def third(self): global num # Here  print(num) # 2  Cls3().third() Cls2().second() Cls1().first() 
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """ # num = 2 # <- Commented class Cls1: num = 3 # <- ✖  def first(self): num = 4 # <- ✖  class Cls2: num = 5 # <- ✖  def second(self): num = 6 # <- ✖  class Cls3: num = 7 # <- ✖  def third(self): global num # NameError: name 'num' is not defined.  print(num) # Did you mean: 'self.num'?  Cls3().third() Cls2().second() Cls1().first() 
Enter fullscreen mode Exit fullscreen mode

<Read(Unintuitive version)>:

""" It's from the viewpoint of `third()` """ class Cls1: def first(self): class Cls2: def second(self): class Cls3: def third(self): global num # Here  print(num) # 2  num = 7 # <- ✖  num = 6 # <- ✖  Cls3().third() num = 5 # <- ✖  num = 4 # <- ✖  Cls2().second() num = 3 # <- ✖ num = 2 # <- 〇 Cls1().first() 
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """ class Cls1: def first(self): class Cls2: def second(self): class Cls3: def third(self): global num # NameError: name 'num' is not defined.  print(num) # Did you mean: 'self.num'?  num = 7 # <- ✖  num = 6 # <- ✖  Cls3().third() num = 5 # <- ✖  num = 4 # <- ✖  Cls2().second() num = 3 # <- ✖ # num = 2 # <- Commented Cls1().first() 
Enter fullscreen mode Exit fullscreen mode

<Change(Intuitive version)>:

""" It's from the viewpoint of `third()` """ num = 2 # <- 〇 class Cls1: num = 3 # <- ✖  def first(self): num = 4 # <- ✖  class Cls2: num = 5 # <- ✖  def second(self): num = 6 # <- ✖  class Cls3: num = 7 # <- ✖  def third(self): global num # Here  num += 10 # Here  print(num) # 12  Cls3().third() print(num) # 6  Cls2().second() print(num) # 4 Cls1().first() print(num) # 12 
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """ # num = 2 # <- Commented class Cls1: num = 3 # <- ✖  def first(self): num = 4 # <- ✖  class Cls2: num = 5 # <- ✖  def second(self): num = 6 # <- ✖  class Cls3: num = 7 # <- ✖  def third(self): global num # NameError: name 'num' is not defined.  num += 10 # Did you mean: 'self.num'?  print(num) Cls3().third() print(num) Cls2().second() print(num) Cls1().first() print(num) 
Enter fullscreen mode Exit fullscreen mode

<Change(Unintuitive version)>:

""" It's from the viewpoint of `third()` """ class Cls1: def first(self): class Cls2: def second(self): class Cls3: def third(self): global num # Here  num += 10 # Here  print(num) # 12  num = 7 # <- ✖  num = 6 # <- ✖  Cls3().third() print(num) # 6  num = 5 # <- ✖  num = 4 # <- ✖  Cls2().second() print(num) # 4  num = 3 # <- ✖ num = 2 # <- 〇 Cls1().first() print(num) # 12 
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """ class Cls1: def first(self): class Cls2: def second(self): class Cls3: def third(self): global num # NameError: name 'num' is not defined.  num += 10 # Did you mean: 'self.num'?  print(num) num = 7 # <- ✖  num = 6 # <- ✖  Cls3().third() print(num) num = 5 # <- ✖  num = 4 # <- ✖  Cls2().second() print(num) num = 3 # <- ✖ # num = 2 # <- Commented Cls1().first() print(num) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)