By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,302 Members | 3,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,302 IT Pros & Developers. It's quick & easy.

Store variable name in another variable

P: n/a
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.

I hope thats clear!!!

How can I do this?

Apr 26 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
loial wrote:
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.

I hope thats clear!!!

How can I do this?
Use a dictionary. Example:

var1=10
var2="abc"
var2=1.2

vardict['var1']=var1
vardict['var2']=var2
vardict['var3']=var3

print vardict['var1']
print vardict['var2']
print vardict['var3']

-Larry
Apr 26 '07 #2

P: n/a
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"]
Apr 26 '07 #3

P: n/a
In article <f0**********@news2.u-psud.fr>,
Laurent Pointal <la*************@limsi.frwrote:
>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.
Apr 26 '07 #4

P: n/a
Cameron Laird wrote:
In article <f0**********@news2.u-psud.fr>,
Laurent Pointal <la*************@limsi.frwrote:
>>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.
Yes, I understand. I just reply to OP question not searching the reason why
he wants to manage *variables* this way (he talk about dict, so I hope he
know that they can be used to map names to values).
So, this is not an advice, just technical ways related to the question as it
was written.

And yes, personnally i use dictionnaries for such purpose.

A+

Laurent.

Apr 26 '07 #5

P: n/a
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name=\""MYVARIABLE"\">
...
...
I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name="JOHN">

Can this be done in Python? Amending the way the variable names are
distinguished in the incoming file is possible if that would help.



On 26 Apr, 22:02, Laurent Pointal <laurent.poin...@wanadoo.frwrote:
Cameron Laird wrote:
In article <f0q89v$ug...@news2.u-psud.fr>,
Laurent Pointal <laurent.poin...@limsi.frwrote:
>loial a ?it :
I need to store a list ofvariablenames 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.

Yes, I understand. I just reply to OP question not searching the reason why
he wants to manage *variables* this way (he talk about dict, so I hope he
know that they can be used to map names to values).
So, this is not an advice, just technical ways related to the question as it
was written.

And yes, personnally i use dictionnaries for such purpose.

A+

Laurent.- Hide quoted text -

- Show quoted text -

May 1 '07 #6

P: n/a
On May 1, 6:43 am, loial <a...@loial.co.ukwrote:
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name=\""MYVARIABLE"\">
..
..

I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name="JOHN">

Can this be done in Python?

s = "hello world, goodbye world"
result = s.replace("world", "moon")
print result

May 1 '07 #7

P: n/a
loial schrieb:
OK, I have it working with dictionaries.

However I have another scenerio :

I am reading a file containing records like the following :

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name=\""MYVARIABLE"\">
..
..
I need to substitute MYVARIABLE with the current value of MYVARIABLE
in my python script and write the file out again.

The file may contain many more lines and many substitution values on
any line

Assuming that MYVARIABLE is currently set to JOHN then the output
would be

<IMPOSITION_DOCUMENT>
<WFBAN_PAGE>
<WFBAN_START_DETAILS name="JOHN">

Can this be done in Python? Amending the way the variable names are
distinguished in the incoming file is possible if that would help.
There are many ways to do so in python. You can use the built-in string
interpolation:

"<tag value="%(name)s"/>" % dict(name=value)

Or you can use one of the many available templating engines, like KID,
genshi, cheetah and so on.

Diez
May 1 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.