[Tutor] Addressing a variable whose name is the value of a string
Kent Johnson
kent37 at tds.net
Mon Apr 9 01:58:33 CEST 2007
John Clark wrote:
> locals()[var]
>
> But I am not sure what the pros/cons for doing something like this would
> be...
locals() should be considered read-only. From the docs:
locals( )
Update and return a dictionary representing the current local
symbol table. Warning: The contents of this dictionary should not be
modified; changes may not affect the values of local variables used by
the interpreter.
At global scope, locals() == globals() and modifying locals() will work.
Within a function, modifying locals() will not do what you want.
Kent
More information about the Tutor
mailing list