Proposed new syntax

Ian Kelly ian.g.kelly at gmail.com
Thu Aug 10 10:44:54 EDT 2017


On Thu, Aug 10, 2017 at 8:28 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> Every few years, the following syntax comes up for discussion, with some people
> saying it isn't obvious what it would do, and others disagreeing and saying
> that it is obvious. So I thought I'd do an informal survey.
>
> 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.

> For comparison, what would you expect this to return? (Without actually trying
> it, thank you.)
>
> [x + 1 for x in (0, 1, 2, 999, 3, 4) if x < 5]

This is already valid syntax. I would expect [1, 2, 3, 4, 5].

> [x + y for x in (0, 1, 2, 999, 3, 4) while x < 5 for y in (100, 200)]

Infinite loop.

> [x + y for x in (0, 1, 2, 999, 3, 4) if x < 5 for y in (100, 200)]

[100, 200, 101, 201, 102, 202, 103, 203, 104, 204]



More information about the Python-list mailing list