186

I have a variable with a string assigned to it and I want to define a new variable based on that string.

foo = "bar"
foo = "something else"   
# What I actually want is:    
bar = "something else"
4
  • 28
    You probably DON'T want that. Why are you trying to do it? Commented Jul 19, 2012 at 4:03
  • 5
    No you don't. The reason you have to use exec is because locals() doesn't support modifications. locals() doesn't support modifications because it would make the implementation more complex and slower and is never a good idea Commented Jul 19, 2012 at 4:04
  • 2
    Similar Post: stackoverflow.com/questions/1373164/… Commented Jul 19, 2012 at 4:08
  • 2
    I landed on this post trying to find out how to assign instance variables for a class using a dictionary. If anybody else has the same problem, you can find a clean solution without exec here: stackoverflow.com/questions/8187082/… Commented Feb 10, 2019 at 1:27

3 Answers 3

302

You can use exec for that:

>>> foo = "bar"
>>> exec(foo + " = 'something else'")
>>> print bar
something else
>>> 
Sign up to request clarification or add additional context in comments.

9 Comments

+1 because he answered the question (as bad an idea as it may be).
@mgalgs why it's a bad idea? Any bad effect this solution have? Thanks in advance.
Note use of exec / eval is considered poor practice. I have not met a single use case where it has been helpful, and there are usually better alternatives such as dict.
why doesn't this work when used in pytest unittests?
+100 what @jpp said. While this answer is useful and correct, if you're writing something other than a one-off script, you can most likely use a dict.
|
179

You will be much happier using a dictionary instead:

my_data = {}
foo = "hello"
my_data[foo] = "goodbye"
assert my_data["hello"] == "goodbye"

7 Comments

It does not seem to address the question.
Yes. This may be helpful, but it wasn't what the OP was asking.
I'm glad to see the answer is still here. I think we can trust Stack Overflow readers to judge for themselves whether an answer suits their needs. This answer does not provide "variable variables," it is true. But I guarantee you that 99% of the people looking for them will be happier to have found dictionaries.
It does not DIRECTLY answer the question, however, people might be asking that question while ignoring they can use a dictionary for the same purpose
"I'm cold, how can I set my house on fire?"
|
130

You can use setattr

name  = 'varname'
value = 'something'

setattr(self, name, value) #equivalent to: self.varname= 'something'

print (self.varname)
#will print 'something'

But, since you should inform an object to receive the new variable, this only works inside classes or modules.

3 Comments

Correct, it only works inside classes.
Can this be used inside Modules, so set a module-level attribute? Eg. MyModule.var
To answer my own question, yes, it can be used from within a module, on itself: stackoverflow.com/questions/2933470/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.