Appending Numbers to Variable Names

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

Appending Numbers to Variable Names

Postby goldcheeta » Fri Jun 14, 2013 5:41 am

Consider the following loop:

Code: Select all
for i in range(0,2):
   a = 2


How would I go about adding the number i after 'a' in the loop so that after running this loop two variables are set to 2, a1 and a2?

I'm using Python 2.7
goldcheeta
 
Posts: 1
Joined: Fri Jun 14, 2013 5:06 am

Re: Appending Numbers to Variable Names

Postby MichelFJM » Fri Jun 14, 2013 7:09 am

Hello

If a1, a2, ... are attributes of a class or class instance, setattr allows that. Else, you can add them to __builtins__ attributes and use them as global variables.
Code: Select all
for i in [1,2] :
     setattr(__builtins__,"a"+str(i),2)

Code: Select all
>>> for i in [1,2] :
...      setattr(__builtins__,"a"+str(i),2)
...
>>> a1
2
>>> a2
2
>>> a3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a3' is not defined

I would be interested if there is an other solution.
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm

Re: Appending Numbers to Variable Names

Postby Mekire » Fri Jun 14, 2013 7:47 am

Ok so firstly it is very unlikely that this is something you should do. Numbered variable names are discouraged in python.
If you want numbered variables put them in a list.

Then instead of names a1 and a2, you would have a[0] and a[1].
You can of course do hacky stuff to make this work but it is terrible:
Code: Select all
>>> for i in range(2):
...     globals()["a{}".format(i+1)] = 2
...     
>>> a1
2
>>> a2
2
>>> 
I STRONGLY encourage you not to even consider doing this.

Then of course there is also the ultimate in unspeakable horrors:
Code: Select all
>>> for i in range(2):
...     exec("a{} = 2".format(i+1))
...     
>>> a1
2
>>> a2
2
>>>
don't even think about it.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Appending Numbers to Variable Names

Postby ochichinyezaboombwa » Fri Jun 14, 2013 5:03 pm

This thread is NC-77.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Appending Numbers to Variable Names

Postby Yoriz » Sat Jun 15, 2013 10:26 am

ochichinyezaboombwa wrote:This thread is NC-77.

Whats that mean ?
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 763
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Appending Numbers to Variable Names

Postby micseydel » Sat Jun 15, 2013 11:19 am

Yoriz wrote:Whats that mean ?

http://en.wikipedia.org/wiki/NC17
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest