Let's write a closure that implements the behavior of a simple counter.
The only non-obvious thing here, is the use of the nonlocal statement. Without using the nonlocal statement, we would get the following error:
UnboundLocalError: local variable 'value' referenced before assignment
The reason for this is:
"...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."
See https://docs.python.org/3/reference/simple_stmts.html#nonlocal

