Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

How do I accomplish variable variables in Python?

Here is an elaborative manual entry, for instance: Variable variables

I hear this is a bad idea in general though, and it is a security hole in PHP. Is that true?

share|improve this question
11  
it's the maintainance and debugging aspects that cause the horror. Imagine trying to find out where variable 'foo' changed when there's no place in your code where you actually change 'foo'. Imagine further that it's someone else's code that you have to maintain... OK, you can go to your happy place now. – glenn jackman Sep 3 '09 at 14:28
2  
The need does still arise, though. I used to think I needed to do this sort of thing all the time before I met real programming languages. Great suggestions here for transitioning to a saner mindset. – Jenn D. Sep 3 '09 at 17:42
    
You can do this in the SAS macro languge. Sometimes you have to, because the only data type in SAS macros is the string :-/ – John Fouhy Sep 3 '09 at 22:07
9  
This is an excellent question to ask, if for no other reason than to help people learn how to avoid it. :) – SunSparc Jul 2 '13 at 21:32
1  
A further pitfall that hasn't been mentioned so far is if such a dynamically-created variable has the same name as a variable used in your logic. You essentially open up your software as a hostage to the input it is given. – holdenweb Dec 19 '14 at 10:50
up vote 75 down vote accepted

Use dictionaries to accomplish this. Dictionaries are stores of keys and values.

>>> dct = {'x': 1, 'y': 2, 'z': 3}
>>> dct
{'y': 2, 'x': 1, 'z': 3}
>>> dct["y"]
2

You can use variable key names to achieve the effect of variable variables without the security risk.

>>> x = "spam"
>>> z = {x: "eggs"}
>>> z["spam"]
'eggs'

Make sense?

share|improve this answer
2  
I'm afraid I don't understand too well. Can you explain? – Pyornide Sep 3 '09 at 12:42
    
    
Edited my answer to explain dictionaries. – chuckharmston Sep 3 '09 at 12:49
    
Yeah, I understand now. Thanks. – Pyornide Sep 3 '09 at 12:54

Use the built-in getattr function to get an attribute on an object by name. Modify the name as needed.

obj.spam = 'eggs'
name = 'spam'
getattr(obj, name)  # returns 'eggs'
share|improve this answer
    
That works great with a namedtuple – kap Jun 26 '15 at 8:29

It's not a good idea. If you are accessing a global variable you can use globals().

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

If you want to access a variable in the local scope you can use locals(), but you cannot assign values to the returned dict.

A better solution is to use getattr or store your variables in a dictionary and then access them by name.

share|improve this answer
6  
+1 answering the question as stated (even though it's horrible) – bobince Sep 3 '09 at 16:22
6  
Don't forget to mention that you can't modify variables through locals() (docs.python.org/library/functions.html#locals). – Glenn Maynard Sep 3 '09 at 18:43

Whenever you want to use variable variables, it's probably better to use a dictionary. So instead of writing

$foo = "bar"
$$foo = "baz"

you write

mydict = {}
foo = "bar"
mydict[foo] = "baz"

This way you won't accidentally overwrite previously existing variables (which is the security aspect) and you can have different "namespaces".

share|improve this answer
    
+1, a good way to explain it. – Nadia Alramli Sep 3 '09 at 12:52
    
Ah. That makes sense. – Pyornide Sep 3 '09 at 12:53

protected by Bhargav Rao yesterday

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?