Dangerous behavior of list(generator)
Peter Otten
__peter__ at web.de
Mon Dec 14 05:35:11 EST 2009
Terry Reedy wrote:
> On 12/13/2009 11:33 PM, exarkun at 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
More information about the Python-list
mailing list