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

אלעזר elazarg at gmail.com
Thu Oct 13 18:08:50 EDT 2016


Trying to restate the proposal, somewhat more formal following Random832
and Paul's suggestion.

I only speak about the single star.
---

*The suggested change of syntax:*

    comprehension ::=  starred_expression comp_for

*Semantics:*

(In the following, f(x) must always evaluate to an iterable)

1. List comprehension:

    result = [*f(x) for x in iterable if cond]

Translates to

    result = []
    for x in iterable:
        if cond:
            result.extend(f(x))

2. Set comprehension:

    result = {*f(x) for x in iterable if cond}

Translates to

    result = set()
    for x in iterable:
        if cond:
            result.update(f(x))

3. Generator expression:

    (*f(x) for x in iterable if cond)

Translates to

    for x in iterable:
        if cond:
            yield from f(x)

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161013/153dfe1e/attachment.html>


More information about the Python-ideas mailing list