Python 101 : Nested function and "non-local" variables


Sometimes we don't want the local variables in nested functions to be interpreted as local variable by python.

Below is a simple example using the variable "var":


As we see the nonlocal var is not "overriden" by the outside var.

If we remove the nonlocal attributes, the outside - more global - var will "override" the value of the inside var.

Comments

Leave as a comment:

Archive