Python indentation

Mark 'Kamikaze' Hughes kamikaze at kuoi.asui.uidaho.edu
Fri Jul 9 20:25:34 CEST 2004


Paramjit Oberoi <p_s_oberoi at hotmail.com>
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:
> http://groups.google.com/groups?q=g:thl470833922d&dq=&hl=en&lr=&ie=UTF-8&selm=yu99isspv9mm.fsf%40europa.research.att.com
> 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.

-1.

  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:

linenum=1
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:
linenum=1
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="http://kuoi.asui.uidaho.edu/~kamikaze/"> 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