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

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Oct 14 02:54:07 EDT 2016


Paul Moore wrote:
> Where in [fn(x) for x in lst if cond] is the *
> allowed? fn(*x)? *fn(x)?

Obviously you're *allowed* to put fn(*x), because that's
already a valid function call, but the only *new* place
we're talking about, and proposing new semantics for, is
in front of the expression representing items to be added
to the list, i.e. [*fn(x) for ...]

> 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.

If we decided to also allow ** in dict comprehensions, then
the expansion would use 'update'.

-- 
Greg


More information about the Python-ideas mailing list