On Sat, Sep 20, 2008 at 11:41 PM, Carl Johnson carl@carlsensei.com wrote:
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.
I imagine what you are thinking of is:
""" def While(x): if x > 10: raise StopIteration else: return x
list(While(x) for x in range(20)) """
This doesn't seem too horrific to me, and certainly doesn't merit adding Break and Continue as Exception-like objects. I feel this would just make it unbearably hard for beginners moving in from <every other language that has looping block constructs and uses break and continue as statements>.
An alternate construct could be something like:
""" def While(x): if x > 10: return None else: return x
[w for w in (While(x) for x in range(20)) if w is not None] """
Of course, this looks silly in this example, but in a context where While were to do something nontrivial, I think it would be suitable.