default tuple unpacking?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Sep 3 20:25:15 EDT 2002


It is often very convenient to have default arguments such as

    def func(a, b, *c): ...

Is there a way to use this in statements too?  Examples:

    a, (b, *c), *d = e
    for a, *b in c: ...
    return a, *b

I often use small utilities such as

    def split1(x): return x[0], tuple(x[1:])
    def split2(x): return x[0], x[1], tuple(x[2:])

to simulate this in easier situations, but I often find the need to do a
more general unpacking by tuple pattern matching.  They would make the code
so much more readable.  Python can already handle all the following

    def func(a, (b,c), *d)
    a, (b, (c, d), e) = f
    for (a,b), (c,d) in e: ...

I would guess it should not be too difficult to handle the combinations.
Any ideas?  Has this been done?


Huaiyu



More information about the Python-list mailing list