Using multiple "*"-style arguments

D

Dave Opstad

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?

Dave
 
A

Andrew Koenig

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.

Does this do what you want?

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

Dave Opstad

"Andrew Koenig said:
Does this do what you want?

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

No, unfortunately, because the arguments in the bounds and b lists are
numeric, so adding them isn't the right thing.

But thanks for the suggestion!
Dave
 
D

Dave Opstad

"Andrew Koenig said:
Does this do what you want?

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

Wait, ignore my previous reply. Yes, I think this does work, and it fits
within the Python idiom I was seeking.

Thanks!
Dave
 
A

Andrew Koenig

Does this do what you want?
x = struct.pack((">HhhhhhhHHHL", i, *(bounds + b[0:2] + b[6:9] +
[len(s)]))

No, unfortunately, because the arguments in the bounds and b lists are
numeric, so adding them isn't the right thing.

Have you tried it? b[0:2] is a list, so + means concatenation, not
addition.
 
J

John Roth

Dave Opstad said:
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("....", * + bounds + b[0:2] + b[6:9] + [len(s)])

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top