Proposed new syntax

Alain Ketterlin alain at universite-de-strasbourg.fr.invalid
Fri Aug 11 06:58:47 EDT 2017


Ian Kelly <ian.g.kelly at gmail.com> writes:

> On Thu, Aug 10, 2017 at 8:28 AM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:

>> What would you expect this syntax to return?
>>
>> [x + 1 for x in (0, 1, 2, 999, 3, 4) while x < 5]
>
> I would expect the for to be an outer loop and the while to be an
> inner, so this would loop infinitely.

+1.

By the way, how is that supposed to be parsed? Is it (sorry for the
braces):

    {for x in for x in (0, 1, 2, 999, 3, 4)}  {while x < 5}

or
    {for x in  {(0, 1, 2, 999, 3, 4) while x < 5}}

I mean: would the "while ..." be another comp_iter [1], or be a
qualifier for an iterable/generator?

If the former, it doesn't make any sense unless you allow side-effects
in the comprehension's expression (as Ian pointed out), and this would
be a very bad idea. If the latter, it could make more sense, similar to
conditional expressions (but I still dislike it, and don't think such a
feature requires syntactic support).

-- Alain.

[1] https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries



More information about the Python-list mailing list