[Python-3000] Using *a for packing in lists and other places

Georg Brandl g.brandl at gmx.net
Sat Mar 15 20:55:23 CET 2008


Guido van Rossum schrieb:
> Thomas Wouters suggests some new syntax:
> 
> http://bugs.python.org/issue2292
> 
>>>> a, b, *c = range(5)
> 
>>>> *a, b, c = a, b, *c
>>>> a, b, c
> ([0, 1, 2], 3, 4)
>>>> [ *a, b, c ]
> [0, 1, 2, 3, 4]
>>>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]
>>>> [ *item for item in L ]
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
> Also, yielding everything from an iterator:
> 
>>>> def flatten(iterables):
> ....     for it in iterables:
> ....         yield *it
> ....
>>>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]
>>>> flatten(L)
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
> What do people think?

As Brett says, it's a good generalization. +1 from me.

Georg


-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-3000 mailing list