DEV Community

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

Posted on • Edited on

global vs nonlocal in Python (9)

Buy Me a Coffee

*Memo:

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(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):
                        print(num) # 6
                Cls3().third()
        Cls2().second()
Cls1().first()
Enter fullscreen mode Exit fullscreen mode
""" 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()
Enter fullscreen mode Exit fullscreen mode
""" 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()
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 # <- 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()
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):
                        print(num) # 6
                    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):
                        print(num) # 4
                    num = 7 # <- ✖
                # num = 6 # <- Commented
                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):
                        print(num) # 2
                    num = 7 # <- ✖
                # num = 6 # <- Commented
                Cls3().third()
            num = 5 # <- ✖
        # num = 4 # <- Commented
        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):
                        print(num) # NameError: name 'num' is not defined
                                   # Did you mean: 'self.num'?
                    num = 7 # <- ✖
                # num = 6 # <- Commented.
                Cls3().third()
            num = 5 # <- ✖
        # num = 4 # <- Commented
        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):
                        num += 10  # UnboundLocalError: cannot access
                        print(num) # local variable 'num' where it is
                Cls3().third()     # not associated with a value
                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):
                        num += 10  # UnboundLocalError: cannot access
                        print(num) # local variable 'num' where it is
                                   # not associated with a value
                    num = 7 # <- ✖
                num = 6 # <- ✖
                Cls3().third()
                print(num)
            num = 5 # <- ✖
        num = 4 # <- ✖
        Cls2().second()
        print(num)
    num = 3 # <- ✖
num = 2 # <- ✖
Cls1().first()
print(num)
Enter fullscreen mode Exit fullscreen mode

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

<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   # SyntaxError: name 'num'
                        nonlocal num # is nonlocal and global
                        print(num)
                Cls3().third()
        Cls2().second()
Cls1().first()
Enter fullscreen mode Exit fullscreen mode
""" 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()
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):
                        global num   # SyntaxError: name 'num'
                        nonlocal num # is nonlocal and global
                        print(num)
                    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):
                        nonlocal num # SyntaxError: name 'num'
                        global num   # is nonlocal and global
                        print(num)
                    num = 7 # <- ✖
                num = 6 # <- ✖
                Cls3().third()
            num = 5 # <- ✖
        num = 4 # <- ✖
        Cls2().second()
    num = 3 # <- ✖
num = 2 # <- ✖
Cls1().first()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)