Store variable name in another variable

Cameron Laird claird at lairds.us
Thu Apr 26 10:53:05 EDT 2007


In article <f0q89v$uge$1 at news2.u-psud.fr>,
Laurent Pointal  <laurent.pointal at limsi.fr> wrote:
>loial a écrit :
>> I need to store a list of variable names in a dictionary or list. I
>> then later need to retrieve the names of the variables and get the
>> values from the named variables. The named variables will already have
>> been created and given a value.
>
>"Named variables will already have been created"... in what namespace ?
>
>Store a list of names -> dict/list (see tutorial)
>
>Then,
>use namespace.name
>or  getattr(namespace,"name")
>or  locals()["name"]
>or  globals()["name"]
>
>

admin, I want to be sure you understand the advice you've been given.
Yes, it is possible to "double dereference" through named variables;
HOWEVER, it is essentially *never* advantageous to do so in application
programming with Python (nor is it in Perl and PHP, despite what many
senior people there teach).  Use a dictionary, perhaps one with
multi-dimensional keys.



More information about the Python-list mailing list