PEP: statements in control structures (Re: Conditional Expressions don't solve the problem)

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Fri Oct 19 04:02:36 CEST 2001


Carl Banks gave some well-thought out objection, which I'd like to summarize
them as:

(1) There may be many statements before the condition, resulting in long
    while-lines. 
(2) The else-clause for while is not necessary for while 1.

But then I was reminded of the examples I collected by searching for break
statements in the source distribution.  Most usages are similar to

./Demo/pdist/makechangelog.py:
--------------------------------------------(1)

    while 1:
        file = getnextfile(f)
        if not file: break
        revs = []
        while 1:
            rev = getnextrev(f, file)
            if not rev:
                break
            revs.append(rev)
        if revs:
            allrevs[len(allrevs):] = revs


which can be changed to:


    while file = getnextfile(f); file:
        revs = []
        while rev = getnextrev(f, file); rev:
            revs.append(rev)
        if revs:
            allrevs[len(allrevs):] = revs


or better still


    while file = getnextfile(f); file:
        revs = []
        while rev = getnextrev(f, file); rev:
            revs.append(rev)
        allrevs += revs


This is more readable to me, because the condition is on the same line as
while (esp. in a color editor).  In practical terms I do not see why this is
ugly. 


>I would suggest something more along these line (fleshing out the
>example a little):
>
>    suppose:
>        m = re.search (somthing)
>    if m:
>        do_something_with (m)
>    elsuppose:
>        m = re.search (somthing_else)
>    if m:
>        do_something_else_with (m)
>    else:
>        raise some_exception;


If we are to add more keywords, maybe this?

    A
    branch:
        B1
        if C1:
            D1
    else:
        B2
        if C2:
            D2
    else:
        B3
    E

or even this?

    A
    B1
    if C1:
        D1
    else:
        B2
    andif C2:
        D2
    else:
        B3
    E


But I don't think any new keyword for this purpose will ever get in.


>I would suggest something like:
>
>    do:
>        <statements>
>    while <condition>:
>        <statements>
>
>with the do part optional.  The exact keywords and semantics I don't
>care about.  But, like I said, I have no problem with "while 1:".

This is cute, but it cannot work unless the current while statement is
phased out.  Otherwise, if I'm looking at a piece of code like

   ....
   ....
while ...
   ....
   ....

it would not be clear if the while is in a new loop or previous loop.
Presumably this would be allowed

do:
    A
while B:
    C
while D:
    ...


Huaiyu




More information about the Python-list mailing list