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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Update cookies preferences