while (a=b()) ...

Paul Boddie paulb at infercor.no
Wed May 12 08:37:52 EDT 1999


> Well, why don't we get radical and introduce a new loop type:
> 
> do1:
>     <do stuff>
> do2 <continuation condition>:
>     <do more stuff>
> [else:
>     <after-loop stuff>] # for consistency

What about multiple "exit points"? And what purpose does the "else"
section serve? Can't you just have that code after the loop.

> The original example could then be spelled as follows:
> --------------
> do1:
>     c= curs.fetchone()
> do2 c!=None:
>     # do something with c
>     print c
> --------------
> which is not redundant, less error-prone than while(a=b()) and more
> elegant (IMHO) than while 1: ... if not <cond> break ...

While I did initially miss the equivalent of a do...while construct in
Python, I don't see major benefits of the above over the classical form:

while 1:
    c = curs.fetchone()
    if c == None:
        break
    # do something with c
    print c

This does have the advantage that the indentation is preserved, so
everything still appears in one block, thus emphasising the "containment
of the flow of execution" a lot better. When the indentation gets
disrupted, as in the proposed syntax, it isn't clear at a glance whether
there are one or many loops without taking a closer look. If the
proposed syntax is subsequently modified to address this concern, then
it just ends up as a version of the classical form with different
syntax.

Of course, the "while 1" is a bit "ugly", but then it merely causes the
reader to go looking for the exit points. We could change that to a
single keyword acting as a synonym if it really bothered a significant
number of people, but I don't see too much of a need for that.

> Of course, I do not insist on using those particular keywords :-)))

No, with an indoctrinated aversion to using "break" instead of loop
continuation flags, I think we should introduce a new keyword to
legitimise arbitrary loop termination. I suggest
"open-china-shop-door-for-bull-to-leave".

Paul




More information about the Python-list mailing list