global vs nonlocal in Python (2)
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

global vs nonlocal in Python (2)

Publish Date: Aug 20
0 0

Buy Me a Coffee

*Memos:

With 2 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.
""" 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

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 # <- ✖
    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

<Change>:

""" 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()
            second()
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()
            second()
Cls1().Cls2().first()
print(num)
Enter fullscreen mode Exit fullscreen mode

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

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

<Change>:

""" 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():
                    nonlocal num # Here
                    num += 10    # Here
                    print(num) # 16
                third()
                print(num) # 16
            second()
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode
""" 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 # <- Commemnted
                def third():
                    nonlocal num # Here
                    num += 10    # Here
                    print(num) # 15
                third()
            second()
            print(num) # 15
Cls1().Cls2().first()
Enter fullscreen mode Exit fullscreen mode
""" It's from the viewpoint of `third()` """

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

Comments 0 total

    Add comment