PEP-315 ("do" loop)

Wayne Folta wfolta at
Wed Feb 18 16:26:35 CET 2004

> until x <==> while not x

That was one thought that crossed my mind when I first saw an until 
construct in a language. (Was it PL/I?) So it is a bit of a confusion, 
but at the same time my spider senses were telling me that surely a 
language wouldn't reserve a keyword "until" to simply save someone from 
typing "while not". (Of course, a language like perl might well do 
this, but that's why we use python, eh?)

While it might be easy to confuse how "until" acts, it has the strength 
that it follows the intuitive loop paradigm we're used to in all other 
looping constructs that I know of: what loops is inside the loop. 
Python has "else" extensions to loops that are different from most 
languages, but even here the "else" part does not loop.

PEP-315 creates a different kind of two-part loop where code outside of 
the "while" is repeatedly executed. That will fool people who are used 
to the usual one-part loops. You can claim it's actually a "do" loop, 
but with the condition in the middle of the loop, that doesn't really 
hold water.

At the very least I'd say that you need to choose a word other than 
"while" to avoid this kind of confusion. For example, "do ... until 
..." loop where "until" <==> "while not" so that you know immediately 
what's going on. Otherwise, when you see a "while", you're never sure 
what kind of loop it is until you look above it and determine it's not 
a "do while".

> Nothing about the word "until" implies to me that it delays the loop 
> test on the first go-round. PEP 315's syntax seems more clear to me.

Nothing about "do" implies "do repeatedly". My first guess on what the 
PEP-315 syntax means, based on python's "while ... else" syntax, would 
be that the "do" part is executed and then the while loop is executed 
conditional on the "do" having not been broken out of: the inverse of 
"while ... else".

More information about the Python-list mailing list