[Python-ideas] Fwd: Fwd: Fwd: unpacking generalisations for list comprehension
Greg Ewing
greg.ewing at canterbury.ac.nz
Mon Oct 17 18:56:35 EDT 2016
Steven D'Aprano wrote:
> What I don't believe is:
>
> (1) that the majority of Python programmers (or even a large minority)
> regularly and consistently think of comprehensions as syntactic sugar
> for a completely unrolled list display; rather, I expect that they
> usually think of them as sugar for a for-loop;
You don't have to believe that, because thinking about it
as a for-loop works equally well. Without the star, it means
"insert each of these things into a list". With the star,
it means "unpack each of these things into a list".
> In a list comprehension, we expect the invariant that the number of
> items produced will equal the number of loops performed.
There's a corresponding invariant for list displays: the
number of items produced is equal to the number of expressions
in the display. But that doesn't hold when the display includes
unpacking, for obvious reasons. For the same reasons, we
shouldn't expect it to hold for comprehensions with unpacking.
--
Greg
More information about the Python-ideas
mailing list