for: else: - any practical uses for the else clause?

Paul Rubin http
Sat Sep 30 04:07:44 EDT 2006


Sybren Stuvel <sybrenUSE at YOURthirdtower.com.imagination> writes:
> I must say that the for/else construct is a LOT more readable than the
> rewritten alternatives.

They are all pretty ugly.  I prefer the genexp version with a
hypothetical "is_empty" function:

     all_heights = (block.height for block in stack if block.is_marked())
     if is_empty(all_heights):
        raise SomeError("No marked block")
     heights = sum(all_heights)

Python generators don't really work the right way for this though.

I've been fooling around with Haskell, so these kinds of constructions
seems more natural to me than explicit for loops.



More information about the Python-list mailing list