default tuple unpacking?

Bernhard Herzog bh at intevation.de
Wed Sep 4 09:46:25 EDT 2002


Peter Hansen <peter at engcorp.com> writes:

> Hmm... I think there might be an inconsistency or an unexpected
> result in this.  What if e already contains a tuple?
> 
>    e = (1, 2, 3)
>    *a = e        # a is ((1, 2, 3), )
> 
> That seems sensible, and it's just what happens if you pass
> "e" in to a function defined as "def func(*a)".

No it isn't. If you call func as func(e) the tuple being unpacked into
the function's parameters is (e,) not e. So the situation is more like
calling func as func(*e) so that *a = e would be the same as a = e or
perhaps a = tuple(e)


> Now what about this:
> 
>    a, *b = e     # a is 1, b is (2, 3)
> 
> That's one possible result, and presumably the one you want with
> this syntax, but it's not what you'd get with the function syntax:
> 
>    def func(a, *b):
>      print a
>      print b
> 
>    func(e)

Again the tuple being upacked to a, *b is (e,)...

> Here of course you get (1, 2, 3) in "a" and () in b. 

... so this is obviously what you should get.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list