Using value of a variable as name for a variable

This is the place for queries that don't fit in any of the other categories.

Using value of a variable as name for a variable

Postby nemesis33 » Fri Apr 05, 2013 2:53 pm

Is it possible to assign the value of a variable as the name for a different variable?

This does not work: (python 2.7)
Code: Select all
s = "aaa"

(s) = 1

print aaa


My intension in this example was to create a variable named "aaa" and assign to it the value 1.
nemesis33
 
Posts: 2
Joined: Fri Apr 05, 2013 2:29 pm

Re: Using value of a variable as name for a variable

Postby setrofim » Fri Apr 05, 2013 3:23 pm

No it's not possible (at least, not without major hackery). If you want to do something like, you should probably be using a dict. What are you trying to accomplish?
setrofim
 
Posts: 285
Joined: Mon Mar 04, 2013 7:52 pm

Re: Using value of a variable as name for a variable

Postby micseydel » Fri Apr 05, 2013 9:37 pm

You could use locals()[s], but as setrofim said, it'd be better to use your own dictionary instead of the one Python uses for your scope.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Using value of a variable as name for a variable

Postby nemesis33 » Sat Apr 06, 2013 6:20 pm

Thanks!
nemesis33
 
Posts: 2
Joined: Fri Apr 05, 2013 2:29 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron