[Python-ideas] A "local" pseudo-function
python at mrabarnett.plus.com
Tue May 1 16:24:28 EDT 2018
On 2018-05-01 19:12, Tim Peters wrote:
> > 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.
Ah, yes. I see what you mean.
There's another question that hasn't been asked yet: what should
locals() and globals() return?
More information about the Python-ideas