146

For simplicity this is a stripped down version of what I want to do:

def foo(a):
    # I want to print the value of the variable
    # the name of which is contained in a

I know how to do this in PHP:

function foo($a) {
    echo $$a;
}

global $string = "blah"; // might not need to be global but that's irrelevant
foo("string"); // prints "blah"

Any way to do this?

dsavi
  • 1,723
  • 3
  • 12
  • 8

4 Answers4

232

If it's a global variable, then you can do:

>>> a = 5
>>> globals()['a']
5

A note about the various "eval" solutions: you should be careful with eval, especially if the string you're evaluating comes from a potentially untrusted source -- otherwise, you might end up deleting the entire contents of your disk or something like that if you're given a malicious string.

(If it's not global, then you'll need access to whatever namespace it's defined in. If you don't have that, there's no way you'll be able to access it.)

Edward Loper
  • 15,374
  • 7
  • 43
  • 52
83

Edward Loper's answer only works if the variable is in the current module. To get a value in another module, you can use getattr:

import other
print getattr(other, "name_of_variable")

https://docs.python.org/3/library/functions.html#getattr

Asocia
  • 5,935
  • 2
  • 21
  • 46
eresonance
  • 1,351
  • 11
  • 12
  • 8
    And if you want to use this method to get a value in the same module that you are calling `getattr()`, use `import sys; sys.modules[__name__]` to get a reference to the current module. – Dirk Mar 17 '16 at 12:38
  • 1
    do you have an alternative for python3? – Guillermo Mosse Dec 18 '19 at 09:49
  • @GuillermoMosse no, it should be the same: https://docs.python.org/3.8/library/functions.html#getattr – eresonance Dec 19 '19 at 14:41
52

Assuming that you know the string is safe to evaluate, then eval will give the value of the variable in the current context.

>>> string = "blah"
>>> string
'blah'
>>> x = "string"
>>> eval(x)
'blah'
stark
  • 12,615
  • 3
  • 33
  • 50
  • 3
    [**Do not ever use `eval` (or `exec`) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the data to run arbitrary code on your computer.**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Karl Knechtel Jul 04 '22 at 22:47
20
>>> x=5
>>> print eval('x')
5

tada!

maxm
  • 5,161
  • 7
  • 30
  • 33
  • 16
    Security holes: "**tada!**". – Sapphire_Brick Jul 02 '20 at 20:25
  • @Sapphire_Brick to be fair as long as the str that is being eval is a constant it shouldn't be a big problem. – m0bbin Oct 10 '20 at 04:52
  • 1
    @m0bbin it is still a major security issue, if attackers know what the string is they can search for it in memory and edit it easily – Ethan Apr 27 '22 at 23:35
  • 3
    [**Do not ever use `eval` (or `exec`) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the data to run arbitrary code on your computer.**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Karl Knechtel Jul 04 '22 at 22:48