Chapters
Python114 chapters

ReferenceKeywords

nonlocal

Lets a nested function rebind a name in the function around it.

Signature

nonlocal name

Returns

Nothing.

Example

Python
def make_counter():
    count = 0

    def increment():
        nonlocal count
        count += 1
        return count

    return increment

counter = make_counter()
print(counter(), counter())

Output

1 2

See also