How safe is modifying locals()?

Paul Paterson paulpaterson at users.sourceforge.net
Fri Jul 25 19:28:22 CEST 2003


Terry Reedy wrote:
> "Paul Paterson" <paulpaterson at users.sourceforge.net> wrote in message
>>
>>The Python documentation gives a warning that modifying the contents
> 
> of
> 
>>locals() may not affect local values. Despite the warning, the code
>>seems to work as desired, at least on Python 2.2.
> 
> 
> At module scope, locals() == globals().  Within a function, locals()
> currently is a *copy* of the function's local namespace.  So above
> only works because you made the call from the global (module) context
> and would not if you tested by making call from within a function.
> 

Thanks Terry! I adjusted my tests and now see this behaviour exactly. 
The function itself works but the changes do not propogate out to the 
calling scope. So this approach of using locals() appears to be dead.

Are there any other approaches?

Paul






More information about the Python-list mailing list