Questions about `locals` builtin

Ned Batchelder ned at nedbatchelder.com
Tue Feb 27 06:59:03 EST 2018


On 2/27/18 3:52 AM, Kirill Balunov wrote:
> a.  Is this restriction for locals desirable in the implementation of
> CPython in Python 3?
> b.  Or is it the result of temporary fixes for Python 2?

My understanding is that the behavior of locals() is determined mostly 
by what is convenient for the implementors, so that they can keep 
regular code running as quickly as possible.  The answer to the 
question, "why can't we make locals() work more like I expect?" is, 
"because that would make things slower."
>
> Personally, I find the convenient functionality to update the local symbol
> table inside a function, similar to `globals`.

Can you show us an example of why you would want to update locals 
through locals()?  There might be more natural ways to solve your problem.

--Ned.



More information about the Python-list mailing list