469,344 Members | 6,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

Store variable name in another variable

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
7 2329
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
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
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
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
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
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
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.

Similar topics

12 posts views Thread by Sanjay | last post: by
2 posts views Thread by Geert M | last post: by
36 posts views Thread by Crirus | last post: by
6 posts views Thread by Mike Hoff | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.