PEP: statements in control structures

CpJohnson at edgars.co.za CpJohnson at edgars.co.za
Thu Oct 18 11:42:10 CEST 2001


>  3.2. Better break-else interaction:

>        Python allows an else-clause for the while statement.  The naive way
>        of writing the general loop-and-half in current Python interferes
>        with the else clause.  For example,

>            while x = next(); not x.is_end:
>                y = process(x)
>                if y.is_what_we_are_looking_for(): break
>            else:
>                raise "not found"
>
>        cannot be written in this naive version:
>
>            while 1:
>                x = next()
>                if x.is_end: break
>                y = process(x)
>                if y.is_what_we_are_looking_for(): break
>            else:
>                raise "not found"
>
Sorry to jump in at this late stage.  In the previous example there two two
exits from the loop.
The while statement already defines two exits, a) the controling expression
returns false and, b) the break.
So what's wrong with:-



x = next()
while not x.is_end:
    y = process(x)
    if y.is_what_we_are_looking_for(): break
    x = next()
else:
    raise "not found"





Craig






More information about the Python-list mailing list