[Python-ideas] if-syntax for regular for-loops

Arnaud Delobelle arnodel at googlemail.com
Fri Oct 10 21:51:37 CEST 2008

On 4 Oct 2008, at 00:42, Vitor Bosshard wrote:

>> On Fri, Oct 3, 2008 at 12:33 PM, Andreas Nilsson wrote:
>>> Thanks for the pointer!
>>> I don't buy the argument that newlines automagically improves  
>>> readability
>>> though. You also get increased nesting suggesting something  
>>> interesting is
>>> happening where it isn't and that hurts readability.
>>> And as Vitor said, all other constructions of the form 'for i in  
>>> items' can
>>> have if-conditions attached to them, it's really not that far- 
>>> fetched to
>>> assume that the loop behaves the same way. Consistency good,  
>>> surprises bad.
>> Yeah, I know what you mean, and I kind of liked the idea of adding  
>> the
>> if statement to the for loop (for consistency, if nothing else), but
>> it's been discussed before, and plenty of people have made the same
>> argument.  Probably not worth it.
> Besides consistency I think the one major benefit to for..if
> loops is that often you don't just save a line, but also an  
> indentation
> level (whenever you use the if clause solely as a filter), which
> actually increases readability, specially when whatever you do within
> the loop is
> relatively long, with its own indentations.
> The syntax just feels natural. For example:
> for i in somelist if i.pending:
>    <do stuff>
> I really don't see any disadvantage here.

There is also a problem with parsing as:

* the following is correct:

     for i in L1 if cond else L2:

* Python's grammar is LL(1)


More information about the Python-ideas mailing list