See enter link description here link for information on non-local
.
The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope excluding globals. This is important because the default behavior for binding is to search the local namespace first. The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.
In short, it lets you assign values to a variable in an outer (but non-global) scope.
If you remove the keyword nonlocal
and try your program, you will observe:
inner: nonlocal
outer: local
Program:
def outer():
x = 'local'
def inner():
x='nonlocal'
print('inner:', x)
inner()
print('outer:', x)
outer()