The nonlocal Keyword in Python PRO

Python 3.0 introduces the nonlocal new reserved word. Using this new reserved word we can assign directly to a variable with the same name in the outer scope (instead of creating a new variable).

def a():
    temp = 2
    def b():
        nonlocal temp
        temp = 4
        print("temp inside b ",temp)
    b()
    print("temp inside a ",temp)
a()

The following video clip explains a short code sample that uses the nonlocal reserved word.

Share:

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences