Terry Reedy wrote: > On 12/13/2009 11:33 PM, exar...@twistedmatrix.com wrote: >> This could provide behavior roughly equivalent to the behavior >> of a list comprehension. > > Impossible. The only serious option for consistency is to special case > list comps to also trap StopIteration raised in the expression part, but > the devs decided not to do this as doing do is arguably a bug.
A viable option might be to introduce a different exception type and translate (expr(v) for v in items if cond(v)) into def gen(items, expr, cond): for v in items: try: if cond(v): yield expr(v) except StopIteration: raise TypeError("StopIteration raised in " "'expr' or 'cond' part of " "a generator expression") Peter -- http://mail.python.org/mailman/listinfo/python-list