[Python-3000] PEP 3132: Extended Iterable Unpacking

Christian Heimes lists at cheimes.de
Wed May 2 02:32:35 CEST 2007


Neville Grech Neville Grech wrote:
> This reminds me a lot of haskell/prolog's head/tail list splitting. Looks
> like a good feature.

Agreed!
> a*=range(5)
> hmmn maybe in such a case, whenever there is the * operator, the resulting
> item is always a list/tuple, like the following:
> a=[[0,1,2,3,4]] ?

Did you mean *a = range(5)?
The result is too surprising for me. I would suspect that *a = range(5) 
has the same output as a = range(5).

 >>> *b = (1, 2, 3)
 >>> b
(1, 2, 3)

 >>> a, *b = (1, 2, 3)
 >>> a, b
1, (2, 3)

 >>> *b, c = (1, 2, 3)
 >>> b, c
(1, 2), 3


 >>> a, *b, c = (1, 2, 3)
 >>> a, b, c
1, (2,), 3

But what would happen when the right side is too small?
 >>> a, *b, c = (1, 2)
 >>> a, b, c
1, (), 2

or should it raise an unpack exception?

This should definitely raise an exception
 >>> a, *b, c, d = (1, 2)

Christian



More information about the Python-3000 mailing list