Dangerous behavior of list(generator)

Terry Reedy tjreedy at udel.edu
Sun Dec 13 03:45:39 EST 2009


Tom Machinski wrote:
> In most cases, `list(generator)` works as expected. Thus,
> `list(<generator expression>)` is generally equivalent to `[<generator
> expression>]`.
> 
> Here's a minimal case where this equivalence breaks, causing a serious
> and hard-to-detect bug in a program:
> 
>   >>> def sit(): raise StopIteration()

StopIteration is intended to be used only within the .__next__ method of 
iterators. The devs know that other 'off-label' use results in the 
inconsistency you noted, but their and my view is 'don't do that'.




More information about the Python-list mailing list