<filename>beyond_the_basics/03.closures_an_decorators/demo06_nonlocal_keyword.py
"""Variable scope and the nonlocal keyword demo"""
message = "global"

def enclosing():
    message = "enclosing"

    def local():
        # non local message
        message = "local"

        def locallocal():
            nonlocal message
            message = "locallocal"

        locallocal()
        print(f"local message: {message}")

    print(f"enclosing message: {message}")
    local()
    print(f"enclosing message: {message}")

print(f"global message: {message}")
enclosing()
print(f"global message: {message}")

<filename>beyond_the_basics/03.closures_an_decorators/demo06_nonlocal_keyword.py
"""Variable scope and the nonlocal keyword demo"""
message = "global"

def enclosing():
    message = "enclosing"

    def local():
        # non local message
        message = "local"

        def locallocal():
            nonlocal message
            message = "locallocal"

        locallocal()
        print(f"local message: {message}")

    print(f"enclosing message: {message}")
    local()
    print(f"enclosing message: {message}")

print(f"global message: {message}")
enclosing()
print(f"global message: {message}")

Variable scope and the nonlocal keyword demo # non local message