conditional for-statement

Falcolas garrickp at gmail.com
Tue Aug 25 21:42:34 CEST 2009


On Aug 25, 11:25 am, seb <sdemen... at gmail.com> wrote:
> We could as consistenly explain that the syntax
>
> for n in range(10) if n%3==0:
>   body
>
> means
>
> for n in range(10):
>   if n%3==0:
>     body
>
> This syntax has also the benefit of avoiding an extra level of
> indentation (the one for the if) that bears no real meaning on a
> structural level.
>
> Maybe a PEP could do the job...
>
> Sébastien

So, what part of the statement does the "if" statement belong to;
particularly a concern considering this is valid python:

for x in y if y else z:
    body

You can always do the following at the cost of 6 symbols, and the gain
of clarity:

for n in (x for x in y if y%3==0):
    body

~G



More information about the Python-list mailing list