Store variable name in another variable

L

loial

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?
 
L

Larry Bates

loial said:
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
 
L

Laurent Pointal

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"]
 
C

Cameron Laird

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

Laurent Pointal

Cameron said:
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.
 
L

loial

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.







Cameron said:
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 -
 
7

7stud

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
 
D

Diez B. Roggisch

loial said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top