* for generic unpacking and not just for arguments?

Christian Heimes lists at cheimes.de
Sun Nov 29 11:09:09 EST 2009


Russell Warren wrote:
> but the code below is not?
> 
>>>> x = (3, 4)
>>>> (1, 2, *x) == (1, 2, 3, 4)
> Traceback (most recent call last):
>   File "<string>", line 1, in <fragment>
> invalid syntax: <string>, line 1, pos 8
> 
> Why does it only work when unpacking arguments for a function?  Is it
> because the code below is preferred, and more readable?
> 
>>>> x = (3, 4)
>>>> (1, 2) + x == (1, 2, 3, 4)
> True
> 
> I've rooted around to see if there is an answer already and found some
> threads going way back to 1998 (!!), but can't find a concise answer
> as to why it is limited to args.

The feature is available in Python 3.x:

>>> a, b, *c = 1, 2, 3, 4, 5
>>> a, b, c
(1, 2, [3, 4, 5])
>>> a, *b, c = 1, 2, 3, 4, 5
>>> a, b, c
(1, [2, 3, 4], 5)

Christian




More information about the Python-list mailing list