[Tutor] newbie: help unkludge me

Kent Johnson kent_johnson at skillsoft.com
Fri Oct 22 02:10:04 CEST 2004


Tuple assignment is what you want:

self.person1, self.person2, self.number1, self.number2, self.number3 = set1

You could use a loop with setattr, but it is a bit awkward. Note that the 
list contains strings, the actual attribute names:

names = ('person1', 'person2', 'number1', 'number2', 'self.number3')
for name, value in zip(names, values):
   setattr(self, name, value)

Kent

At 07:05 PM 10/21/2004 -0400, Rene Lopez wrote:
>Here is the code that seems a bit kludgey to me:
>
>class blah
>   def __init__(self)
>       set_table = { 1: ("bob", "bill", 1, 2, 3),
>                          2: ("gill", "sally", 5, 6, 7)}
>
>       wanted = 1
>
>       if  wanted in set_table:
>       set1 = set_table[wanted]
>
>       self.person1 = set1[0]
>       self.person2 = set1[1]
>       self.number1 = set1[2]
>       self.number2 = set1[3]
>       self.number3 = set1[4]
>
>
>Is there a way to assign the info from set1 to the person/number
>variables using a loop or something?   I considered trying something
>like this:
>
>myset = (self.person1, self.person2, self.number1, self.number2, self.number3)
>
>so I could trying copying the data over with something like this:
>
>while  x < 5:
>   myset[x-1] = set1[x-1]
>
>but that doesn't work because I get an error saying that myset has no
>instance of "person1" In other words...they don't exist yet, so they
>can't be put into a list.  I hope this makes some sense, because
>reading it sounds weird even to me :-)
>
>--
>
>Rene
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list