Paul Rubin wrote:

> Peter Otten <[EMAIL PROTECTED]> writes:
>> >      all_heights = (block.height for block in stack if
>> >      block.is_marked()) if is_empty(all_heights):
>> >         raise SomeError("No marked block")
>> 
>> Such a function would have to rebind the generator:
> 
> Yeah, that's what I mean about generators not working the right way.
> 
>> You can make it work, but the result tends to be messy:
> 
> I think it's better underneath, but still syntactically ugly, to just
> defer the generator creation:
> 
>      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.

>      if is_empty(all_heights ()):
>          raise SomeError("No marked block")
>      height = sum(all_heights ())
> 
> Now sum and is_empty see two separate generators, so is_empty is
> straightforward:
> 
>      def is_empty(gen):
>         try:
>            gen.next()
>            return True
>         except StopIteration:
>            return False

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

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

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to