Proposed new syntax

justin walters walters.justin01 at gmail.com
Thu Aug 10 11:41:07 EDT 2017


On Thu, Aug 10, 2017 at 7: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]
>

>>> [1, 2, 3, 4]

>
>
> 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]
>
>
>>> [1, 2, 3, 4, 5]


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

I'd expect this to throw a syntax error.


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

Syntax error again.


>
>
>
> Thanks for your comments!
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list