PEP-xxx: Unification of for statement and list-comp syntax

Heiko Wundram me+python at modelnine.org
Mon May 22 05:39:22 EDT 2006


Am Montag 22 Mai 2006 11:27 schrieb Boris Borcic:
> Mhhh, your unsugared form remind me of darks hours with primitive BASICS in
> my youth - the kind Dijsktra commented on. Why don't you write
>
>         for node in tree:
>             if node.haschildren():
>                 <do something with node>

As I've replied on python-dev, indentation is not always a good thing, 
especially if the for-body is longer than a few lines.

The "if not: continue" form allows you to keep the indentation at one level, 
so that it's pretty clear what is part of the loop body, and what is not. If 
you add an extra indentation, your mind has to keep track of the indentation, 
and will expect an "else:" somewhere, which in the use case I propose won't 
happen. At least that's what my mind does, and is majorly confused, if the 
else doesn't appear.

This certainly is personal taste, and AFAICT there are pretty few people who 
feel like I do. But, basically, I find it easier to have one level of 
indentation, and to test for the negated condition, than to put the loop body 
in an enclosing if-statement, which will always add an extra level of 
indentation. I put forth the proposal, because it allows you to save this 
level of indentation, which makes the code more readable for me.

Anyway, the PEP has already been rejected on python-dev, and I'm currently 
just rewriting it with the positive and negative things that have so far been 
said, so basically, it'll just be there so that people can be pointed at it 
when anybody else'll ask for it.

--- Heiko.



More information about the Python-list mailing list