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

Paul Moore p.f.moore at gmail.com
Sat Oct 15 09:06:16 EDT 2016


On 14 October 2016 at 10:48, Paul Moore <p.f.moore at gmail.com> wrote:
> On 14 October 2016 at 07:54, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>> I think it's probably time for someone to
>>> describe the precise syntax (as BNF, like the syntax in the Python
>>> docs at
>>> https://docs.python.org/3.6/reference/expressions.html#displays-for-lists-sets-and-dictionaries
>>
>>
>> Replace
>>
>>    comprehension ::=  expression comp_for
>>
>> with
>>
>>    comprehension ::=  (expression | "*" expression) comp_for
>>
>>> and semantics (as an explanation of how to
>>> rewrite any syntactically valid display as a loop).
>>
>>
>> The expansion of the "*" case is the same as currently except
>> that 'append' is replaced by 'extend' in a list comprehension,
>> 'yield' is replaced by 'yield from' in a generator
>> comprehension.
[...]
> So now I understand what's being proposed, which is good. I don't
> (personally) find it very intuitive, although I'm completely capable
> of using the rules given to establish what it means. In practical
> terms, I'd be unlikely to use or recommend it - not because of
> anything specific about the proposal, just because it's "confusing". I
> would say the same about [(x, *y, z) for ...].

Thinking some more about this, is it not true that

[ *expression for var in iterable ]

is the same as

[ x for var in iterable for x in expression ]

?

If so, then this proposal adds no new expressiveness, merely a certain
amount of "compactness". Which isn't necessarily a bad thing, but it's
clearly controversial whether the compact version is more readable /
"intuitive" in this case. Given the lack of any clear improvement, I'd
be inclined to think that "explicit is better than implicit" applies
here, and reject the new proposal.

Paul.


More information about the Python-ideas mailing list