[Python-ideas] if condition: break idiom

Carl Johnson carl at carlsensei.com
Sun Sep 21 05:41:10 CEST 2008


Slightly OT, but I think 'break' and 'continue' should be replaced  
with 'raise Break' and 'raise Continue' in Python 4000, just as we  
'raise StopIteration' in generators today. This would be handy,  
because you could use it in functions called by a list comprehension:

def while(x):
     if x > 10:
         raise Break
     else:
         return x

[while(x) for x in range(20)]  #produces [0, 1, 2, 3, 4, 5, 6, 7, 8,  
9, 10]

Right now, you can do something this with list(generator expression)  
by raising StopIteration, but it is considered a hack, and it doesn't  
work with list comprehensions. 



More information about the Python-ideas mailing list