The nonlocal Keyword in Python


Use nonlocal within nested functions to modify variables in the nearest enclosing scope.

Source Code

def outer():
    x = "local"
    def inner():
        nonlocal x
        x = "nonlocal"
    inner()
    print(x)  # nonlocal
outer()
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments