Questions about `locals` builtin

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Feb 28 06:58:45 EST 2018


On Wed, 28 Feb 2018 18:04:11 +1100, Chris Angelico wrote:

> On Wed, Feb 28, 2018 at 5:54 PM, dieter <dieter at handshake.de> wrote:
[...]
>> 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.
>>
>>
> That seems like a hairy thing to do, honestly.

Well, yeah, but it is during debugging, not production code.


> But if you know that
> there's only a handful of variables that you'd actually want to do that
> to, you can simply put those into an object of some form, and then
> mutate that object.

o_O

If I knew ahead of time which variables held the wrong value, I'd fix 
them so they held the right value ahead of time, and not bother using a 
debugger.

:-)



-- 
Steve




More information about the Python-list mailing list