How do I do this? (eval() on the left hand side)

Peter Otten __peter__ at web.de
Thu Dec 9 02:46:59 EST 2004


Peter Hansen wrote:

> Caleb Hattingh wrote:
>> I am convinced now that locals() doesn't work as (I) expected.  Steven
>> says there was some or other reason why locals() as used in this
>> context  is not writable - Do you know why this is?  I really do not
>> like  guidelines like "may not work", "is unreliable" and so on.
>> Perhaps this  is a character flaw, but I really do like to know what
>> works, when it  works, and when it doesn't work.
> 
> Those who've talked about it being "unreliable" are misstating
> the Rule that you are looking for.  I'll quote it below, so
> that you aren't left in this unfortunate state of limbo:
> 
> The Rule of locals()
>     Updating locals() should not be done.  Treat the
>     return value of locals() as read-only.  Never try
>     to update it.  End of story.
> 
> Anything that appears to suggest that locals() might sometimes
> actually be writable is not really happening.  Look the other
> way.  Pay no attention to the man behind the curtain.  And
> especially, whatever else you do, don't let the PSU se

I agree. But much of the confusion stems from interpreter experiments like

>>> locals()["a"] = 42
>>> a
42

That would go away if locals() returned an ignore-write proxy where the
global and local scope are identical. The ability to "just try it" is an
asset.

Peter






More information about the Python-list mailing list