[Python-ideas] if condition: break idiom

Arnaud Delobelle arnodel at googlemail.com
Sun Sep 21 23:10:39 CEST 2008


On 21 Sep 2008, at 21:24, Josiah Carlson wrote:
[...]
> Really though, what is pretty/elegant really varies depending on what
> those ... turn into.  To be honest, I've only ever used the above once
> or twice, though the standard first idiom I've used dozens of times.
> Though if you really want to have fun, there's nothing like exploiting
> while loops for checking error conditions. ;)
>
> def foo(...):
>    first = 1
>    while first:
>        first = 0
>        if error_condition1:
>            break
>        ... #other error conditions, processing, whatever
>    else:
>        #only executes if everything is ok
>    #error condition
>

Why not write:

def foo(...):
     while True:
         if error_condition1:
             break
         # Other error conditions, processing, etc
         # only executes if everything is OK
     # Error condition

Is there something that I don't see?

-- 
Arnaud




More information about the Python-ideas mailing list