[Python-Dev] [SPAM?] Re: PEP 558: Defined semantics for locals()
Guido van Rossum
guido at python.org
Tue May 28 21:02:19 EDT 2019
On Tue, May 28, 2019 at 5:25 PM Greg Ewing <greg.ewing at canterbury.ac.nz>
> Terry Reedy wrote:
> > I believe that the situation is or can be thought of as this: there is
> > exactly 1 function locals dict. Initially, it is empty and inaccessible
> > (unusable) from code. Each locals() call updates the dict to a current
> > snapshot and returns it.
> Yes, I understand *what's* happening, but not *why* it was designed
> that way. Would it really be prohibitively expensive to create a
> fresh dict each time?
No. But it would be inconsistent with the behavior at module level.
FWIW I am leaning more and more to the [proxy] model, where locals() and
frame.f_locals are the same object, which *proxies* the fast locals and
cells. That only has one downside: it no longer returns a dict, but merely
a MutableMapping. But why would code care about the difference? (There used
to be some relevant builtins that took dicts but not general
MutableMappings -- but that has been fixed long ago.)
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him/his **(why is my pronoun here?)*
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev