converting from perl: variable sized unpack

Quinn Dunkan quinn at hork.ugcs.caltech.edu
Sun Jul 15 23:46:04 EDT 2001


On Mon, 16 Jul 2001 01:03:29 GMT, Tim Hammerquist <tim at vegeta.ath.cx> wrote:
>Me parece que Quinn Dunkan <quinn at yak.ugcs.caltech.edu> dijo:
>> Ruby allows * notation:
>> 
>> a, b, *c = sequence # `c' gets the rest of the sequence
>> 
>> which is cute, but I don't like it.  Just another random "convenient" little
>> trick to remember.  It's not half as useful as the 1.6 * apply trick.  Let's
>> go easy on the syntax gimmicks.
>
>I'm relatively new to Python. What's the '1.6 * apply' trick?

As of python 1.6,

f(x, *y, **z)

is shorthand for

apply(f, (x,), y, z)

In other words, you can "unpack" sequence or keyword args into a function
without writing out the apply stuff.  It's useful because it helps a common
idiom:

class Base(Super):
    def __init__(self, x, y, *rest, **kw):
        Super.__init__(*rest, **kw)
        # Base handles `x' and `y'

I personally don't think the shorthand is worth the syntactic baggage, but I
don't use it (or, more importantly, see it used) very much.  I'm sure some
other people love it and use it every day.



More information about the Python-list mailing list