DEV Community

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

Posted on • Edited on

global vs nonlocal in Python (4)

Buy Me a Coffee

*Memo:

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

*Memo:

  • 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
    class Cls2:
        num = 4 # <- Class variable
        print(num) # 4
        def first(self):
            num = 5 # <- Non-local variable
            print(num) # 5
            def second():
                num = 6 # <- Non-local variable
                print(num) # 6
                def third():
                    num = 7 # <- Local variable
                    print(num) # 7
                third()
            second()
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode

<Unintuitive version>:

""" It's from the viewpoint of `third()` """

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

# num = 2 # <- Commented
class Cls1:
    num = 3 # <- ✖
    class Cls2:
        num = 4 # <- ✖
        def first(self):
            num = 5 # <- ✖
            def second():
                num = 6 # <- ✖
                def third():
                    global num # NameError: name 'num' is not defined.
                    print(num) # Did you mean: 'sum'?
                third()
            second()
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode

<Read(Unintuitive version)>:

""" It's from the viewpoint of `third()` """

class Cls1:
    class Cls2:
        def first(self):
            def second():
                def third():
                    global num # Here
                    print(num) # 2
                num = 6 # <- ✖
                third()
            num = 5 # <- ✖
            second()
        num = 4 # <- ✖
    num = 3 # <- ✖
num = 2 # <- 〇
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """

class Cls1:
    class Cls2:
        def first(self):
            def second():
                def third():
                    global num # NameError: name 'num' is not defined.
                    print(num) # Did you mean: 'sum'?
                third()
                num = 6 # <- ✖
            num = 5 # <- ✖
            second()
        num = 4 # <- ✖
    num = 3 # <- ✖
# num = 2 # <- Commented
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode

<Change(Intuitive version)>:

""" It's from the viewpoint of `third()` """

num = 2 # <- 〇
class Cls1:
    num = 3 # <- ✖
    class Cls2:
        num = 4 # <- ✖
        def first(self):
            num = 5 # <- ✖
            def second():
                num = 6 # <- ✖
                def third():
                    global num # Here
                    num += 10  # Here
                    print(num) # 12
                third()
                print(num) # 6
            second()
            print(num) # 5
Cls1().Cls2().first()
print(num) # 12
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """

# num = 2 # <- Commented
class Cls1:
    num = 3 # <- ✖
    class Cls2:
        num = 4 # <- ✖
        def first(self):
            num = 5 # <- ✖
            def second():
                num = 6 # <- ✖
                def third():
                    global num # NameError: name 'num' is not defined.
                    num += 10  # Did you mean: 'sum'?
                    print(num)
                third()
                print(num)
            second()
            print(num)
Cls1().Cls2().first()
print(num)
Enter fullscreen mode Exit fullscreen mode

<Change(Unintuitive version)>:

""" It's from the viewpoint of `third()` """

class Cls1:
    class Cls2:
        def first(self):
            def second():
                def third():
                    global num # Here
                    num += 10  # Here
                    print(num) # 12
                num = 6 # <- ✖
                third()
                print(num) # 6
            num = 5 # <- ✖
            second()
            print(num) # 5
        num = 4 # <- ✖
    num = 3 # <- ✖
num = 2 # <- 〇
Cls1().Cls2().first()
print(num) # 12
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """

class Cls1:
    class Cls2:
        def first(self):
            def second():
                def third():
                    global num # NameError: name 'num' is not defined.
                    num += 10  # Did you mean: 'sum'?
                    print(num)
                num = 6 # <- ✖
                third()
                print(num)
            num = 5 # <- ✖
            second()
            print(num)
        num = 4 # <- ✖
    num = 3 # <- ✖
# num = 2 # <- Commented
Cls1().Cls2().first()
print(num)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)