Questions about `locals` builtin

dieter dieter at handshake.de
Wed Feb 28 01:54:30 EST 2018


Ned Batchelder <ned at nedbatchelder.com> writes:
> 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.

I am still working with Python 2 (Python 3 may behave differently).
There, during debugging, I would sometimes like to change the value
of variables (I know that the variable has got a wrong value
and would like to fix it to continue senseful debugging without a restart).
This works for variables not yet known inside the function but does
not work for true local variables.
I assume that this is one effect of the "locals()" restriction.




More information about the Python-list mailing list