nonlocal

say it: “non-LOH-kul” /ˌnɑnˈloʊkəl/

Lets an inner function change a variable from the function around it.

What does nonlocal do?

nonlocal lets an inner function change a variable that belongs to the function around it, instead of making a new local one.

def make_counter():
    count = 0
    def click():
        nonlocal count
        count += 1
        return count
    return click

counter = make_counter()
counter(); print(counter())  # 2

Level: Freelancer · More Python terms at this level

Updated