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

Thomas Wouters thomas at python.org
Sat Mar 15 22:18:10 CET 2008


On Sat, Mar 15, 2008 at 2:05 PM, BJörn Lindqvist <bjourne at gmail.com> wrote:
> On Sat, Mar 15, 2008 at 4:15 PM, Guido van Rossum <guido at python.org>
> wrote:
> > >>> 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]
>> Can you mix freely?
>> >>> [*(*item, *item) for item in L]
> [0, 1, 2, 0, 1, 2, 3, 4, 3, 4, 5, 5, 6, 6, 7, 8, 9, 7, 8, 9]
>
Of course. It's just a generalization of *, it's not a special case.
[*(*item, *item)] is automatically parsed correctly. Of course, since L
contains a generator, the output isn't entirely like you suggested:
 >>> a = [0, 1, 2]
>>> L = [ a, (3, 4), {5}, {6: None}, (i for i in range(7, 10)) ]
>>> [*(*item, *item) for item in L]
[0, 1, 2, 0, 1, 2, 3, 4, 3, 4, 5, 5, 6, 6, 7, 8, 9]
The only thing that doesn't work (yet) is:
list(*item for item in L)
because the grammar gets confused about the '*item'. This is caused by the
specialcasing that *args and **kwargs in functioncalls and -definitions
really is. Since Guido wants to be able to do 'f(*a, b, c)' (and presumably
'f(*a, *b, *c)' too) that will all have to change anyway. However, changing
this part of the grammar is, uhm, "not as easy", so I need a couple of days.
-- 
Thomas Wouters <thomas at python.org>
Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20080315/f645792c/attachment.htm 


More information about the Python-3000 mailing list

AltStyle によって変換されたページ (->オリジナル) /