Using multiple "*"-style arguments

John Roth newsgroups at jhrothjr.com
Fri Jun 18 11:56:50 EDT 2004


"Dave Opstad" <dave.opstad at agfamonotype.com> wrote in message
news:dave.opstad-D50FCC.07252818062004 at reader0901.news.uu.net...
> File this one under "enhancement request" I guess, but it would sure be
> nice. I wrote this line the other day, thinking it would just work:
>
> x = struct.pack(">HhhhhhhHHHL", i, *bounds, *b[0:2], *b[6:9], len(s))
>
> Unfortunately, it doesn't. The only valid place for an "*"-style
> argument is at the end of the arguments list. But this line seems so
> natural! Since struct.pack expects all its arguments separately (rather
> than gathered into a list or tuple), I have to be able to accommodate
> its needs, and since I had several arguments already in lists, I thought
> I could get away with this notation.
>
> Certainly I can get around this limitation by either spelling out the
> arguments (i.e. b[0], b[1], b[6]...), or by writing multiple lines of
> code to gather the arguments into a single list which could then have
> the "*" applied. But I'd love a simpler, more compact notation, not
> unlike what I've written above. Does anyone know of a current Python
> idiom for unravelling list arguments in calls?

While I haven't tested it, I suspect that this might work:

x = struct.pack("....", *[i] + bounds + b[0:2] + b[6:9] + [len(s)])

John Roth
>
> Dave





More information about the Python-list mailing list