No Do while/repeat until looping construct in python?

Giovanni Bajo noway at sorry.com
Wed Mar 12 13:37:42 CET 2003


"John Roth" <johnroth at ameritech.net> ha scritto nel messaggio
news:v6u9b9e2iant6a at news.supernews.com...

> 1. Python tends toward minimalism; there is usually lots of
> resistance to adding constructs when there isn't a real clear
> use case, and there are real clear and useful alternatives.
> See the debate (aka "flame war") over PEP 308.

Sure, when requesting additions, we are not discussing about
Turing-completeness. There are many ways you can do things in Python. I
understand that e.g. list comprehension is a rather new addition to the
language. While I like them, it's just syntactic sugar. Do/while constructs
is a clear use case, there are many situations where you want your loop to
execute at least once, and have your exit condition checked at the end.

> In this connection, the 'break' statement will handle the
> case where you actually want the loop condition at the end,
> at the expense of less than a half dozen additional keystrokes.

Then why do we have a loop condition at the start? It would have sufficed to
have a "while True" construct only, and use breaks to evaluate conditions
wherever you want.
Actually, the only reason I can see that exaplains why do/while is missing
is that python does not have any block closing statement (it uses
indentation for that), so there would be no obvious place to put your final
condition test, unless you consider this readable:

while_check_at_the_end t>0:

which of course I don't.

> 2. Python tends toward stating things clearly; that is, up front.
> Putting the loop condition at the end is nowhere near as clear
> as putting it up front.

Debatable. If it's a post-condition, there is no way you can put it upfront.
And anyway, a clean loop construct would be cleaner than:

while True:   # look, I'm lying you!
    <code>
    if a>0:      # here is the loop condition!
        break

Of course, this is probably a 'good-enough' compromise, so nobody really
cares.

> 3. Many of the actual use cases for a 'decision at the end'
> loop are also served by the for statement, generators and
> iterators.

Uh. I don't want to implement an iterator, so that I can use a for loop,
when just a do/while loop would suffice (or whileTrue/break).

Giovanni Bajo






More information about the Python-list mailing list