[Python-ideas] if condition: break idiom

Andrew Clover and-dev at doxdesk.com
Thu Sep 25 15:34:05 CEST 2008


Arnaud Delobelle wrote:

> while True:
>     do something
>     if condition: break
>     do something else

I think mid-test/multi-test loops are common enough that it might be 
worth explicit syntax to pick out the loop exit points:

     while True:
         do something
     andwhile not condition:
         do something else

(or 'and while' if one wanted to save on reserved words. 'not condition' 
because the logic is reversed from the quoted example.)

Similarly for 'if', I often find myself with unsatisfactory constructs like:

     if condition:
        do something
        if othercondition:
            success
        else:
            failure
     else:
         failure

I would like to flatten together the else clauses:

     if condition:
         do something
     andif othercondition:
         success
     else:
         failure



More information about the Python-ideas mailing list