Using multiple "*"-style arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Opstad

    Using multiple "*"-style arguments

    File this one under "enhancemen t request" I guess, but it would sure be
    nice. I wrote this line the other day, thinking it would just work:

    x = struct.pack(">H hhhhhhHHHL", 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
  • Andrew Koenig

    #2
    Re: Using multiple "*&quot ;-style arguments

    "Dave Opstad" <dave.opstad@ag famonotype.com> wrote in message
    news:dave.opsta d-D50FCC.07252818 062004@reader09 01.news.uu.net. ..
    [color=blue]
    > File this one under "enhancemen t request" I guess, but it would sure be
    > nice. I wrote this line the other day, thinking it would just work:
    >
    > x = struct.pack(">H hhhhhhHHHL", 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.[/color]

    Does this do what you want?

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


    Comment

    • Dave Opstad

      #3
      Re: Using multiple &quot;*&quot ;-style arguments

      In article <C2DAc.2081$OB3 .55@bgtnsc05-news.ops.worldn et.att.net>,
      "Andrew Koenig" <ark@acm.org> wrote:
      [color=blue]
      > Does this do what you want?
      >
      > x = struct.pack(("> HhhhhhhHHHL", i, *(bounds + b[0:2] + b[6:9] +
      > [len(s)]))[/color]

      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

      Comment

      • Dave Opstad

        #4
        Re: Using multiple &quot;*&quot ;-style arguments

        In article <C2DAc.2081$OB3 .55@bgtnsc05-news.ops.worldn et.att.net>,
        "Andrew Koenig" <ark@acm.org> wrote:
        [color=blue]
        > Does this do what you want?
        >
        > x = struct.pack(("> HhhhhhhHHHL", i, *(bounds + b[0:2] + b[6:9] +
        > [len(s)]))[/color]

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

        Thanks!
        Dave

        Comment

        • Andrew Koenig

          #5
          Re: Using multiple &quot;*&quot ;-style arguments

          > > Does this do what you want?[color=blue][color=green]
          > >
          > > x = struct.pack(("> HhhhhhhHHHL", i, *(bounds + b[0:2] + b[6:9] +
          > > [len(s)]))[/color]
          >
          > No, unfortunately, because the arguments in the bounds and b lists are
          > numeric, so adding them isn't the right thing.[/color]

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


          Comment

          • John Roth

            #6
            Re: Using multiple &quot;*&quot ;-style arguments


            "Dave Opstad" <dave.opstad@ag famonotype.com> wrote in message
            news:dave.opsta d-D50FCC.07252818 062004@reader09 01.news.uu.net. ..[color=blue]
            > File this one under "enhancemen t request" I guess, but it would sure be
            > nice. I wrote this line the other day, thinking it would just work:
            >
            > x = struct.pack(">H hhhhhhHHHL", 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?[/color]

            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[color=blue]
            >
            > Dave[/color]


            Comment

            Working...