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()) 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 -- http://mail.python.org/mailman/listinfo/python-list