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

Paul Rubin http
Sat Sep 30 08:04:02 EDT 2006


Peter Otten <__peter__ at web.de> writes:
> >      all_heights = lambda:
> >                      (block.height for block in stack if
> >                      block.is_marked())
> 
> You still need the stop() trick to omit the heights after the marked block.

Yeah, that code was based on my earlier mis-read of the original post.
How's this:

    def blocks_until_mark():
       return itertools.takewhile(lambda block:\
                                      not block.is_marked(), \
                                  stack)

    height = sum(b.height for b in blocks_until_mark())
    if is_empty(blocks_until_mark()):
        raise SomeError("No marked block")

> Alternatively you can turn all_heights into a list (comprehension) which
> makes the test trivial. 

Yes, I felt it wasn't in the spirit of the thing to use that memory.

> I think it will be interesting to see how Python 3000's emphasis on
> iterators will affect overall code complexity.

We will need more iterator primitives, which are still evolving.



More information about the Python-list mailing list