PEP 315: Enhanced While Loop
Jordan McCoy
jordanm at uts.cc.utexas.edu
Sat May 3 12:39:37 EDT 2003
The main issue I find with the proposed syntax is that it misrepresents
the purpose of <setup code>. Reference:
> This PEP proposes to solve these problems by adding an optional
> clause to the while loop, which allows the setup code to be
> expressed in a natural way:
>
> do:
> <setup code>
> while <condition>:
> <loop body>
>
> This keeps the loop condition with the while keyword where it
> belongs, and does not require code to be duplicated.
My immediate intuition, when presented with the above syntax, is to
assume that the actual logic of the loop occurs in <setup code>, for two
reasons:
1. It follows from the meaning of 'do' and 'while': "I'm eating lunch
('do') while the diner is serving it ('while')." The above syntax
corresponds with: "I'm in the diner ('do'), while they are serving lunch
('while') and I'm eating lunch ('loop body')." The second example makes
sense, but the first one is more intuitive and natural.
2. Most major languages that implement a post-test loop include the
<loop body> between 'do' and 'while' (or their equivalents). People new
to Python but familar with such languages will expect this.
A closer examination of the code in <setup code> and <loop body> will of
course indicate what is happening. But why create dissonance? My
suggestion would be to use another word, perhaps 'with':
with:
<setup code>
while <condition>:
<loop body>
and perhaps:
with:
<setup code>
do:
<loop body>
while <condition>
I also liked the posttest suggestion, as it keeps the loop condition on
top and clearly indicates the behavior of the loop. Though, in the
interest of completeness, I would suggest an optional, default 'pretest'
keyword as well. So we would have:
with:
<setup code>
posttest while <condition>:
<loop body>
I tend to agree with most of the other posters in that this possibly
adds nice but probably unnecessary syntax and may have indention-flow
implications. Further, 'with' may not be the best word either. However,
I prefer clean, intuitive language constructs where necessary, even if
duplicative of certain uses of other constructs, as I believe it
increases readability and makes the behavior of the code more apparent
and elegant.
Explicitly indicating the setup code of a loop appears worthwhile; I
just don't think 'do' indicates this behavior.
--
Jordan McCoy
(jordanm at uts.cc.utexas.edu)
More information about the Python-list
mailing list