[Python-ideas] if condition: break idiom

Josiah Carlson josiah.carlson at gmail.com
Thu Sep 25 18:54:18 CEST 2008


On Thu, Sep 25, 2008 at 6:34 AM, Andrew Clover <and-dev at doxdesk.com> wrote:
> 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
>

That breaks the flow of the loop, and it is not visually clear that
"do something else" is a part of the while loop.  This is one of many
reasons why switch/case statements failed.


> (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

That becomes ambiguous in the case of more than 2 levels of if, never
mind needing andelif, never mind being wholly unreadable by
non-english speakers.  Flat is better than nested, but nested indents
do offer clarity about what code does.  A brief skimming of the above
tells me nothing about the control flow of the code.

 - Josiah



More information about the Python-ideas mailing list