The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope.
See the following examples without and with nonlocal
>>> def outer():
x = 1
def inner():
x = 2
print("inner:", x)
inner()
print("outer:", x)
>>> outer()
inner: 2
outer: 1
To this, using nonlocal:
>>> def outer():
x = 1
def inner():
nonlocal x
x = 2
print("inner:", x)
inner()
print("outer:", x)
>>> outer()
inner: 2
outer: 2