[Python-ideas] Starred expression in right-hand side

Arnaud Delobelle arnodel at googlemail.com
Thu Jun 26 08:06:38 CEST 2008


On 26 Jun 2008, at 05:36, David Pokorny wrote:

> Since PEP 3132 gives us:
>
>>>> x = [1,2,3]
>>>> a, *b = x
>>>> a
> 1
>>>> b
> [2, 3]
>
> it seems natural that we should be able to do it the other way too:
>
> (doesn't actually work)
>>>> a, b = 1, [2,3]
>>>> x = [a,*b]
>>>> x
> [1, 2, 3]
>
> This is essentially itertools.chain, but of course it isn't nearly  
> as much fun:
>
>>>> [n for n in itertools.chain([1],[2,3])]
> [1, 2, 3]
>
> Now you might be thinking, "yeah, that's cool, but you don't really
> need it" but this actually came up in practice: I have a function that
> has a certain behavior for standard types, but when it sees a type it
> doesn't recognize, it calls a protocol function (like __iter__ or
> __next__) and expects to receive an iterable whose 0th element is a
> string. Now I'm probably not going to use itertools.chain because the
> only members of itertools I can reliably remember are count() and
> izip(), so my next alternative (which is perfectly acceptable) is
>
>>>> a, b = 1, [2,3]
>>>> x = [a] + b
>
> This isn't too bad, but it is slightly less clear than x = [a,*b] and
> much less efficient when b is long.

What makes you think it is much less efficient?

-- 
Arnaud




More information about the Python-ideas mailing list