Is there a more elegant way to do this?
Peter Otten
__peter__ at web.de
Sat Jun 26 18:15:48 EDT 2004
Kamilche wrote:
> '''
> Is there a more elegant way of doing this?
> I would like to have the arguments to pack
> automatically taken from lst.
> '''
>
> def pack(self):
> lst = ['id', 'parent', 'number', 'x', 'y', 'z', 'red', \
> 'green', 'blue', 'size', 'rotate', 'translucency']
> return struct.pack('<IIIiiiBBBHHH', \
> self.id, self.parent, self.number, \
> self.x, self.y, self.z, \
> self.red, self.green, self.blue, \
> self.size, self.rotate, self.translucency)
There is an alternative
struct.pack("<IIIiiiBBBHHH", *[getattr(self, name) for name in lst])
and you could even add the typecodes
lst = [("I", 'id'), ("I", 'parent'), ... ]
struct.pack("<" + "".join([t for (t, n) in lst]),
*[getattr(self, n) for (t, n) in lst])
but frankly, I prefer your original version.
Peter
More information about the Python-list
mailing list