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

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Oct 16 00:44:51 EDT 2016


Steven D'Aprano wrote:

> This thread is a huge, multi-day proof that people do not agree that 
> this is a "reasonable" interpretation.

So far I've seen one very vocal person who disgrees, and
maybe one other who isn't sure.

> This proposal only makes even a little 
> bit of sense if you imagine list comprehensions
> 
>     [*t for a in it1 for b in it2 for c in it3 ... for t in itN]
> 
> completely unrolled into a list display:
> 
>     [*t, *t, *t, *t, ... ]
> 
> but who does that?  Why would you reason about your list comps like that?

Many people do, and it's a perfectly valid way to think
about them. They're meant to admit a declarative reading;
that's the reason they exist in the first place.

The expansion in terms of for-loops and appends is just
*one* way to describe the current semantics. It's not
written on stone tablets brought down from a mountain.
Any other way of thinking about it that gives the same
result is equally valid.

> magically adds an second invisible for-loop to your 
> list comps:

You might as well say that the existing * in a list
display magically inserts a for-loop into it. You can
think of it that way if you want, but you don't have
to.

> it is intentionally
> prohibited because it doesn't make sense in the context of list comps.

I don't know why it's currently prohibited. You would
have to ask whoever put that code in, otherwise you're
just guessing about the motivation.

-- 
Greg


More information about the Python-ideas mailing list