let's say I have a variable called "x" and a string that has the value of "x" (string1 = "x"). How do I do stuff with the variable through the string? For example change the variable's value or call a method if it's an object? Thanks in advance
-
I'm not sure I understand your question, but "x" and "string1" are both different references so you can just change "x" however you want. So long as you don't change "string1" again, its value won't change. See this link for details on Pythons "pass by object" (pass by value if it's immutable) https://www.python-course.eu/passing_arguments.php – RoboBear Sep 21 '18 at 19:32
-
1"How do I do stuff with the variable through the string? " Don't. this is bad design. – juanpa.arrivillaga Sep 21 '18 at 19:37
-
@RoboBear the evaluation strategy does not change with immutable/mutable objects. It always works **exactly the same**. – juanpa.arrivillaga Sep 21 '18 at 19:41
-
Read [this](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html) as to why you shouldn't do this. – juanpa.arrivillaga Sep 21 '18 at 19:42
-
Let me emphasize the "don't". If I'm going through a potential hire's public codebase and see this happening without an *exceptionally* compelling use case (which is to say, basically, required-for-interop), their CV is going in the circular file, since it indicates knowing disregard for best practices. – Charles Duffy Sep 21 '18 at 19:50
-
I think it's also worth mentioning that you *can* use locals() or globals(), but in an Enterprise application with testing, linting or internationalization, it's not really a good practice to use this IMO. See https://stackoverflow.com/questions/1550479/python-is-using-vars-locals-a-good-practice for more discussion. – RoboBear Sep 21 '18 at 19:56
3 Answers
Variables are available through dictionaries locals()
and globals()
. If you want to access a particular variable by it's spring name, you can do e.g.
>>> my_var = 'hello'
>>> x = 'my_var'
>>> locals()[x]
'hello'
You can also assign back to the variable using this approach, e.g.
>>> my_var = 'hello'
>>> x = 'my_var'
>>> locals()[x] = 'something else'
>>> my_var
'something else'
Since functions are objects in Python, you can access any locally available functions in the same manner to call them.
>>> def my_test_function(n):
>>> return n*8
Accessing the method and calling it.
>>> locals()['my_test_function'](4)
32
For accessing attributes of objects by their name you can use getattr()
, and setattr()
to set them. For example, creating an object with a single property called your_prop
.
class Example:
your_prop = 2
a = Example()
The value is available via your_prop
.
>>> a.your_prop
2
The property can be accessed via name using getattr
>>> getattr(a, 'your_prop')
2
The property can be set using setattr
:
>>> setattr(a, 'your_prop', 5)
>>> a.your_prop
5

- 15,275
- 7
- 50
- 70
-
1
-
@spiderboy great! Updated answer with an example for doing the same on objects. – mfitzp Sep 21 '18 at 19:40
Ok, let's suppose that you have lots of different functions: Aoo()
, Boo()
, Coo()
... and let's suppose that you want to specify which of them to call via command line argument.
Now, that argument will be a string, so you need to call a function through its name, but you do not know in advance the name of the function.
One possible solution is to use exec()
:
def boo():
print("boo function")
def coo():
print("coo function")
Now:
argument = "boo"
exec(argument + "()")
>>> boo function
and
argument = "coo"
exec(argument + "()")
>>> coo function

- 2,270
- 1
- 12
- 22
It depends what you're trying to do, but you can scoop up whatever x
is pointing to with locals()
or globals()
:
def x(k):
return k + 1
string1 = "x"
the_function_x = locals()[string1]
print(the_function_x(3))
outputs 4
(it called the x
function by utilizing string1
).

- 12,939
- 6
- 51
- 52