954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

setting a string as a variable name

I just started using python a couple of days ago, so I am a complete beginner. Basically what I am trying to do is the following. Imagine that I have a string variable such as:

food = 'bread'

I want to create another variable (integer or another type) so that its name is 'bread', in other words the new variable name should be the value of the string variable "food". How can I do such an assignment? My guess is that it should not take more than two or three lines. I read up and am thinking that I need to use a function called setattr, but am not able to figure out how to use it.

Thank you in advance.

jopeto
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Probably the easiest way to do this is to use the local variable dictionary vars() to create new variables on the fly:

food = 'bread'

vars()[food] = 123

print bread  # --> 123


You can print out vars() to see what's going on.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

I tried what you have above; it seems the vars() statement in line 3 requires quotes around 'food':

vars()['food'] = 123

Otherwise works perfect.

lukescp
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

I tried what you have above; it seems the vars() statement in line 3 requires quotes around 'food':

vars()['food'] = 123

Otherwise works perfect.

That is fine if you want to do this:

vars()['food'] = 123
print food   # --> 123


The original code wanted to print bread like this:

food = 'bread'
vars()[food] = 123
print bread  # --> 123


There is a difference there!

Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

Now how about in methods to classes where you want to create a variable named 'self.whatever'? It works fine outside of Class methods, but I'm sure there's a way to do this. I've tried

vars()['self.test']


and

vars()[self+'.test']


. Is it possible?

mcsweenus
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Now how about in methods to classes where you want to create a variable named 'self.whatever'? It works fine outside of Class methods, but I'm sure there's a way to do this. I've tried

vars()['self.test']


and

vars()[self+'.test']


Is it possible?

mcsweenus
Newbie Poster
2 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
class sample(object):
    def __init__(self):
        vars(self)['a'] = 10

s = sample()
print s.a
>> 10


Hope this helps.

--Karthik Jayapal

karthick.ajk
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 0
 

This helped me too thanks

DragonReeper
Newbie Poster
6 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You