[Python-ideas] if condition: break idiom

Arnaud Delobelle arnodel at googlemail.com
Sat Sep 20 21:54:59 CEST 2008

On 20 Sep 2008, at 20:07, Brett Cannon wrote:

> On Sat, Sep 20, 2008 at 11:35 AM, Arnaud Delobelle
> <arnodel at googlemail.com> wrote:
>> [...] I think that this would look better:
>> while True:
>>   do something
>>   break if condition
>>   do something else
> For some reason this suggestion reminds me of Icon.
> Anyway, so I take it this suggestion is to extend the break statement
> to have an 'if' trailer? Or are you trying to make 'break' an
> expression?

No I wasn't suggesting this.

> If you are after the former, the problem with that is that the
> special-casing of 'if' might confuse people because they will suddenly
> want an 'else' clause. Adding that might then cause a naive user to
> see ``break if x`` and then think 'break' is an expression.

I thought that the meaning of e.g.

     break if x < 1

was clear and unambiguous.

I don't know if some people do, but I personally have never felt the  
need to write something like:

     expression1 if condition else expression2

on its own on a line.  So I wouldn't find the above 'break if x < 1'  
statement confusing :)

> Which leads to its own issues. How do you implement 'break' as an
> expression? It doesn't make much sense to have ``fxn(a, break, b)``
> inside a loop. And you can't say 'break' can only appear in the true
> clause of an 'if' expression as that is too specialized and will lead
> to potential misuse of the statement (or at least an attempt).

I certainly wouldn't want 'break' to be an expression.

> I don't see enough benefit from the change to warrant dealing with any
> of the above issues.

Fair enough!

> -Brett


More information about the Python-ideas mailing list