PEP 315: Enhanced While Loop

Andrew Koenig ark at research.att.com
Wed May 7 07:44:11 EDT 2003


Marcus> while <condition1>:
Marcus> 	<code1>
Marcus> 	break unless <condition2>
Marcus> 	<code2>
Marcus> 	break unless <condition3>
Marcus> 	<code3>

Marcus> Too Perlish?-) But it does read rather good, in my humble opinion.

I think I disagree.  The point is that the multiple exits should be
considered normal exits, not abnormal ones.  The difference appears
when you add an "else" clause:

        while:
            line = infile.getline()
        and while line:
            <process the line>
            if <something is wrong>:  break
        else:
            <code to execute after normal exit>

If the loop terminates normally -- that is, because we reached the end
of the file -- I would like the code after "else:" to execute.  If,
on the other hand, it terminates because of the break, I would like the
code after "else:" not to execute.

I think this behavior is analogous to the current usage:

        for line in infile:
            <process the line>
            if <something is wrong>:  break
        else:
            <code to execute after normal exit>

I'm sure someone will correct me if I'm wrong :-)

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list