On 9/1/2020 5:58 PM, Andras Tantos wrote:

On 9/1/2020 12:41 PM, MRAB wrote:

CPython is able to identify all of the local names of a function and,

Note >>>of a function<<<.

basically, for reasons of efficiency, it uses slots for the local names instead of an actual dict. 'locals()' just returns a dict that represents those local names and their current values, but modifying that dict has no effect on the actual local names. In short, there isn't really a local dict that you can replace.

This applies to code in a function (def statement or lambda expression) and only that.

[Snip experiments with non-function code at >>> prompt.]

In other words, I can indeed change the value of a local variable through the locals() dict.

This is true for code that is not part of a function body, which is top level code and class code that is not part of a method body.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to