[Python-ideas] A "local" pseudo-function
tim.peters at gmail.com
Tue May 1 14:12:13 EDT 2018
> Imagine renaming the specified names that are declared 'local' throughout
> the nested portion:
> def f():
> a = 10
> local local_a:
> def showa():
> print("a is", local_a)
> showa() # Raises NameError in showa because nothing bound to local_a yet.
> local_a = 20
> showa() # 20
> local_a = 30
> showa() # Raises NameError in f because local_a not defined.
In a later message I showed executable-today Python code that I
believe models your intent. That code does indeed display "30" for
the last call, and while I myself don't see that it's _useful_ yet,
the model is incomprehensible if it doesn't.
It doesn't matter that local_a isn't defined at function scope,
because showa() refers to the locals _in_ the "local a:" scope. The
last value bound to local_a was 30, so that's what showa() needs to
show. That the name `showa` is _bound_ in f's locals has nothing to
do with whose locals showa() _uses_. Other current messages about
"closures" go on more about that.
More information about the Python-ideas