PEP-315 ("do" loop)

rzed rzantow at ntelos.net
Tue Feb 17 21:19:44 EST 2004


Wayne Folta <wfolta at netmail.to> wrote in
news:mailman.84.1077049175.31398.python-list at python.org: 

>> 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".
> 
> 

Nothing about loops is necessarily obvious, but the "until" 
convention in PL/I (and proposed here)probably takes four seconds 
to learn and understand completely. It's not a difficult concept: 
"while" checks at loop start, "until" checks at loop end. That's 
it.

It does allow a natural way to express a loop's intent (do 
something one or more times, stopping when this-test is true), and 
it could also allow specification of *both* a before-test and 
after-test in the same statement (while this-is-true do the 
following, until that-is-true), which gives a clean way to express 
the intent of the loop block that requires both tests. 
Syntactically, I'd rather see:

while cond1 until cond2:
    some code

... than: 

while cond1:
    some code
    if cond2: break

... though I can live with either. If "some code" takes a few 
screens, I may not realize that the cond2 test is important in the 
loop until I've already scrolled past the part that sets up the 
test. It's not a deal-breaker, but it can be an annoyance when 
looking into other folks' code.

As to the PEP itself, It would take supstantially longer than four 
seconds to get completely comfortable with the loop-and-a-half 
construct using any syntax, I'd bet. *That* I'd rather see spelled 
out explicitly, but that's just me.

-- 
rzed




More information about the Python-list mailing list