How to get the value of a variable given its name in a string?

python get variable name
python get name of variable passed to function
python dynamic variable name
variable name to string
how to add string to variable name python
get string version of variable name python
python print variable name and value
python treat variable name as string

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?


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

Python: access variable from string, Note that the lookup.get (instead of just lookup[blurb] handles the case where a new object named "myvariable" (or to set "myvariable" to a new value) for name in mylist: print '%s : %r' % (name, getattr(modobj, name)). 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'


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/2/library/functions.html#getattr

Getting the name of a variable as a string, Using the python-varname package, you can easily retrieve the name of the variables. https://github.com/pwwang/python-varname. In your  I am reading the name of a string variable from the database (e.g. "_datafile"). I want to know how I can access a named variable within my program using this string. I have already tried using a


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'

How to get a variable name as a string in Python?, The only objects in Python that have canonical names are mo The iteritems returns the list of all variables in the scope with their values. the chosen dataset will now be stored in the variable 'dataset' and you can use it as per normal. You could also store all the datasets as fields of a struct and use the variable name string to access the correct field of the struct.


>>> x=5
>>> print eval('x')
5

tada!

String Tutorials & Notes | Python, How do you assign a variable to a name in Python? varname = genvarname(str) constructs a string or character vector varname that is similar to or the same as the str input, and can be used as a valid variable name. str can be a string, a string array, a character array, a cell array of character vectors. If str is a string array or cell array of character vectors, genvarname returns a string array or cell array of character vectors in varname.


Convert string to a variable name, How do I convert a string to a variable name in R? There is no direct way to get the name of a variable as a string, but there is a function called "inputname" that will return the name of a variable that was input into a function:


Set-Variable, PS C:\> Set-Variable -Name "processes" -Value (Get-Process) -Option constant -​Scope global -Description "All processes" -PassThru | Format-List Type: String Specifies an array of items that this cmdlet excludes from the operation. Is it possible in VBA to determine a variable name from a string? For example - - - - - - - Dim VariableName as String Dim FavoriteNumber as Integer FavoriteNumber = 55 VariableName = "FavoriteNumber" msgbox {*} - - - - - - * indicating an expression that will return the value of the FavoriteNumber variable, using the value of VariableName


String variables, Like numeric variables, string variables can have labels, and missing value variables are frequently used to identify observations in a scatterplot, name the also a number of specific functions that perform operations on string variables. Gets the variables in the current console. The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name. This command gets variables with names that begin with the letter m.