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.
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