Python indentation

Mark 'Kamikaze' Hughes kamikaze at
Fri Jul 9 20:25:34 CEST 2004

Paramjit Oberoi <p_s_oberoi at>
wrote on Fri, 09 Jul 2004 09:12:42 -0500:
>>> Yes, and I would also be interested in an other proposal
>>> that went something like:
>>> while condition1:
>>>   code
>>> and while condition2:
>>>   code
>>> and while condition3:
>>>   code
> Andrew Koenig proposed this in the following email:
> If link doesn't work - it was a post dated 2003-05-05 with the subject
> "Re: PEP 315: Enhanced While Loop".  There was a fairly long thread about
> it back then.


  I do occasionally write loops where I do something, test and maybe
break, and then go on to do something else.  However, the current while
construct does that just fine, and clearly expresses the start and end
of the code block:

while True:
    line = raw_input()
    if not line: break  # or put on its own line, depending on taste
    print "%05d:%s" % (linenum, line,)
    linenum += 1

  This confuses me as to where the loop starts and ends:
while True:
    line = raw_input()
and while line:
    print "%05d:%s" % (linenum, line,)
    linenum += 1

  I rarely use post-loop condition testing even in Java where it exists
and you can use assignment as an expression, let alone in Python.  When
I do, an explicit if...break at the end works fine.  Any loop that's
more than a few lines long, I usually extract into its own method.

 <a href=""> Mark Hughes </a>
"The void breathed hard on my heart, turning its illusions to ice, shattering
 them.  Was reborn, then, free to scrawl own design on this morally blank
 world.  Was Rorschach." --Alan Moore, _Watchmen #6_, "The Abyss Gazes Also"

More information about the Python-list mailing list