Is there a more elegant way to do this?

Peter Hansen peter at
Tue Jun 29 06:33:40 CEST 2004

Kamilche wrote:

> Peter Hansen <peter at> wrote in message news:<OYydnadytZWWhn3dRVn-ug at>...
>>def pack(self):
>>     args = ('Iid Iparent Inumber ix iy iz Bred Bgreen Bblue '
>>         'Hsize Hrotate Htranslucency')
>>     lst = zip(*[(x[0],x[1:]) for x in s.split()])
>>     return struct.pack('<' + ''.join(lst[0]),
>>         *[getattr(self, n) for n in lst[1]])
> You scary c-like-code-creator, you. :-O

More like Perl, I think, which packs all the unreadability of C
into one tenth the space.

Actually, I thought someone would comment on the emergent
Hungarianesque notation in the above...  I think if I
were doing this for real (and I might well if I had the
need), I'd use a decent separator to make the args string
more readable, maybe just 'I-id I-parent I-robot...' or
something.  That makes the (x[0], x[1:]) turn into the
somewhat cleaner x.split('-').

Another actually: zip I find just plain incomprehensible,
but it sure comes in handy from time to time.  I can't
seem to fit it in my brain though, and constantly resort
to the interactive prompt to figure out how to use it.


More information about the Python-list mailing list