PEP 315: Enhanced While Loop
W Isaac Carroll
icarroll at pobox.com
Mon May 5 07:30:04 EDT 2003
Here are my responses to the ideas discussed since I posted PEP 315.
* PEP was submitted to be rejected
If the Python community rejects the PEP so be it, but I really would
like a statement that works like this.
* "Python ain't broken. Please don't fix it."
Python is not broken. But if Python could not be improved we wouldn't
need PEPs at all.
* "do" is unclear
* do-while in other languages doesn't have 2nd suite
* do-while-while ambiguity
* unseen "do" changes the meaning of seen "while"
These are problems which I had not considered when writing the PEP. It
seems to me that the best way to solve them is to choose different
keywords. How about:
perform:
<setup code>
whilst <condition>:
<loop body>
:)
* indentation should indicate execution path
* it's not clear that both blocks execute each iteration
These are good points. There is no precedent in Python for two
indentation groups which are conceptually the same block. Indenting the
while statement to the same level as the rest of the block would solve
this problem but it would hide the loop condition. I prefer the pattern
recommended in the PEP because it clearly indicates the loop condition,
but I do not know for certain that it is better (clearer, more pythonic,
etc).
* a post-test loop is sufficient
The problem I presented in the PEP could not be solved with a post-test
loop. However, please note that a post-test loop could easily be
constructed using the do-while syntax of the PEP.
* infinite loop with break is a well-known idiom in other languages and
is a good way of expressing this behavior
Experienced programmers are used to writing infinite loops and breaking
out of them. However, saying "while True" does not clearly express the
purpose and behavior of the loop. Just because it's traditional to write
infinite loops does not mean that it's a good way to program.
I feel that the use of break is similar to the use of goto in the past.
Goto was necessary because of the limited control structures available
in early languages, but as for, while, if-else, functions, and other
powerful control structures came into use, the goto was no longer
necessary and gradually disappeared. If we adopt loop constructs of
sufficient power and flexibility, the break statement will also no
longer be needed, and will disappear from use.
In other words, if all you have is an infinite loop, everything looks
like it needs one.
If I have not adequately responded to your comment, please re-post it or
send me email.
TTFN
More information about the Python-list
mailing list