Variable by label

R

rocco.rossi

Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.
 
A

Arnaud Delobelle

Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:

foo = some_function("foo")

or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.

Thank you.

There are the globals() and locals() builtins.
 
R

rocco.rossi

a = 1
b = eval("a")
b 1
a =1
b = globals()["a"]
b 1
a =1
b = locals()["a"]
b


Is there any function which will return a variable by passing to it
the string containing the variable's name? Something like this for
instance:
foo = some_function("foo")
or perhaps it's a dictionary. I don't know, but I would really like to
find out how it can be done. I guess it is achievable.
Thank you.

Perfect. Thank you all for your replies.
 

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

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top