EDIT 2 : since so many people are crying against the bad design this usecase can reveal. Readers of these question and answers should think twice before using it
I’ve trying to set a variable (not property) by it’s name in Python :
foo = 'bar'
thefunctionimlookingfor('foo', 'baz')
print foot #should print baz
PS : the function to access a variable by its name (without eval) would be a plus !
EDIT : I do know dictionary exists, this kind of usage is discouraged, I’ve choose to use it for a very specific purpose (config file modification according to environment), that will let my code easier to read.
Dynamically setting variables in the local scope is not possible in Python 2.x without using
exec, and not possible at all in Python 3.x. You can change the global scope by modifying the dictionary returned byglobals(), but you actually shouldn’t. Simply use your own dictionary instead.