What does the Python nonlocal
statement do (in Python 3.0 and later)?
There's no documentation on the official Python website and help("nonlocal")
does not work, either.
What does the Python nonlocal
statement do (in Python 3.0 and later)?
There's no documentation on the official Python website and help("nonlocal")
does not work, either.
My personal understanding of the "nonlocal" statement (and do excuse me as I am new to Python and Programming in general) is that the "nonlocal" is a way to use the Global functionality within iterated functions rather than the body of the code itself. A Global statement between functions if you will.
Quote from the Python 3 Reference:
As said in the reference, in case of several nested functions only variable in the nearest enclosing function is modified:
The "nearest" variable can be several levels away:
But it cannot be a global variable:
In short, it lets you assign values to a variable in an outer (but non-global) scope. See PEP 3104 for all the gory details.
with 'nonlocal' inner functions(ie;nested inner functions) can get read & 'write' permission for that specific variable of the outer parent function. And nonlocal can be used only inside inner functions eg:
Compare this, without using
nonlocal
:To this, using
nonlocal
, whereinner()
'sx
is now alsoouter()
'sx
: