PEP-315 ("do" loop)

Erik Max Francis max at
Tue Feb 17 22:00:36 CET 2004

Wayne Folta wrote:

> 1. It's clever, addresses a definite "wart", and is syntactically
> similar to try/except. But it's syntax seems like an acquired taste to
> me.

It's also analogous to the way do...while structures are written in
other languages, as well, so it lends familiarity by import.

I personally support the PEP, particularly with its generality.

> In a neater way? How about, instead, creating an "until" loop:
> until line == "\n":
>     line = sys.stdin.readline()
[inserted indentation manually, didn't come through here]
> Would be defined to work exactly like a while loop except the test is
> not evaluated the first time through the loop. Wouldn't this be akin
> to checking at the end of the loop, while maintaining a more while-ish
> syntax?

The problem is it doesn't read like it runs.  The suite is executed
_first_, then the test is repeatedly done and the suite is executed if
it is correct, like a while loop.  In some sense you have some "voodoo
magic" here because code is not executed in the order you write it. 
It's much better when it's written in the order it is executed, at least
as much as is reasonable.  The do...while form does that well.

Also, `until' isn't the right word here.  "Until x, do y" just means to
repeatedly do y until x becomes true.  In analogue with "while x, do y,"
at first glance all it seems is that the `until' construct does the
logical negation of the conditional expression, but nothing else; that
is, it would seem logical to conclude that

	until condition:

is precisely the same as

	while not condition:

which is not at all what you mean.  This form of `until' is indeed
included in other languages like Perl.  (That doesn't mean that Perl's
way is the right way, it's just that this is an area which would be ripe
for confusion.)

You could use some keyword other than `until', but I think the confusion
of ordering is still there.  Something gets executed, then a while loop
occurs, the best way to do that is to have that something appear above
the while loop, and that's just what the do...while form accomplishes.

> Is this at all useful or is something of the order of PEP-315 the way
> to go?

I think the PEP 315 method is superior.

 __ Erik Max Francis && max at &&
/  \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Sometimes life gets in the way / You'll survive, be strongest
    -- Des'ree

More information about the Python-list mailing list