global vs nonlocal in Python (3)



This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee☕

*Memos:

First of all, there are 4 kinds of variables from the viewpoint of third() using 3 classes and 3 functions 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.
""" 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()

A global statement can refer to a global variable as shown below. *The doc explains the rules for local and global variables in Python:

<Read>:

""" 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()
""" 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()

<Change>:

""" 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()
        Cls2().second()
Cls1().first()
print(num) # 12
""" 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()
        Cls2().second()
Cls1().first()
print(num)

A nonlocal statement can refer to a non-local variable as shown below:

<Read>:

""" 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):
                        nonlocal num # Here
                        print(num) # 6
                Cls3().third()
        Cls2().second()
Cls1().first()
""" 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 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        nonlocal num # Here
                        print(num) # 4
                Cls3().third()
        Cls2().second()
Cls1().first()
""" It's from the viewpoint of `third()` """

num = 2 # <- ✖
class Cls1:
    num = 3 # <- ✖
    def first(self):
        # num = 4 # <- Commented
        class Cls2:
            num = 5 # <- ✖
            def second(self):
                # num = 6 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        nonlocal num # SyntaxError: no binding
                        print(num)   # for nonlocal 'num' found
                Cls3().third()
        Cls2().second()
Cls1().first()

<Change>:

""" 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):
                        nonlocal num # Here
                        num += 10    # Here
                        print(num) # 16
                Cls3().third()
                print(num) # 16
        Cls2().second()
Cls1().first()
""" 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 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        nonlocal num # Here
                        num += 10    # Here
                        print(num) # 14
                Cls3().third()
        Cls2().second()
        print(num) # 14
Cls1().first()
""" It's from the viewpoint of `third()` """

num = 2 # <- ✖
class Cls1:
    num = 3 # <- ✖
    def first(self):
        # num = 4 # <- Commented
        class Cls2:
            num = 5 # <- ✖
            def second(self):
                # num = 6 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        nonlocal num # SyntaxError: no binding
                        num += 10    # for nonlocal 'num' found
                        print(num)
                Cls3().third()
        Cls2().second()
        print(num)
Cls1().first()

Without a global or nonlocal statement, the closest non-local variable or a global variable can be referred to in order as shown below:

<Read>:

""" 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):
                        print(num) # 6
                Cls3().third()
        Cls2().second()
Cls1().first()
""" 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 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        print(num) # 4
                Cls3().third()
        Cls2().second()
Cls1().first()
""" It's from the viewpoint of `third()` """

num = 2 # <- 〇
class Cls1:
    num = 3 # <- ✖
    def first(self):
        # num = 4 # <- Commented
        class Cls2:
            num = 5 # <- ✖
            def second(self):
                # num = 6 # <- Commented
                class Cls3:
                    num = 7 # <- ✖
                    def third(self):
                        print(num) # 2
                Cls3().third()
        Cls2().second()
Cls1().first()
""" It's from the viewpoint of `third()` """

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

<Change>:

""" 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):
                        num += 10  # UnboundLocalError: cannot access
                        print(num) # local variable 'num' where it is
                Cls3().third()     # not associated with a value
        Cls2().second()
Cls1().first()

Using both a global and nonlocal statement in the same function gets error as shown below:

""" 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   # SyntaxError: name 'num'
                        nonlocal num # is nonlocal and global
                        print(num)
                Cls3().third()
        Cls2().second()
Cls1().first()
""" 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):
                        nonlocal num # SyntaxError: name 'num'
                        global num   # is nonlocal and global
                        print(num)
                Cls3().third()
        Cls2().second()
Cls1().first()


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)