[Python-ideas] Fwd: Fwd: unpacking generalisations for list comprehension

Steven D'Aprano steve at pearwood.info
Sat Oct 15 04:36:30 EDT 2016


On Fri, Oct 14, 2016 at 06:23:32PM +1300, Greg Ewing wrote:

> To maintain the identity
> 
>   list(*x for x in y) == [*x for x in y]
> 
> it would be necessary for the *x in (*x for x in y) to expand
> to "yield from x".

Oh man, you're not even trying to be persuasive any more. You're just 
assuming the result that you want, then declaring that it is 
"necessary".  :-(


I have a counter proposal: suppose *x is expanded to the string literal 
"Nope!". Then, given y = (1, 2, 3) (say):

    list(*x for x in y)

gives ["Nope!", "Nope!", "Nope!"], and 

    [*x for x in y]

also gives ["Nope!", "Nope!", "Nope!"]. Thus the identity is kept, and 
your claim of "necessity" is disproven.

We already know what *x should expand to: nearly everywhere else, *x is 
conceptually replaced by a comma-separated sequence of the items of x. 
That applies to function calls, sequence unpacking and list displays. 

The only exceptions I can think of are *args parameters in function 
parameter lists, and sequence packing on the left side of an assignment, 
both of which work in similar fashions.

But not this proposal: it wouldn't work like either of the above, hence 
it would be yet another unrelated use of the * operator for some 
special meaning.



-- 
Steve


More information about the Python-ideas mailing list