[Python-3000] Is this a bug with list comprehensions or not?

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Jul 11 01:47:56 CEST 2008


Antoine Pitrou wrote:

> That is, in your example:
> 
>  >>> def f(x):
> ...  if x > 5: raise StopIteration
> ...
>  >>> list(x for x in range(100) if not f(x))
> [0, 1, 2, 3, 4, 5]
> 
> f() certainly shouldn't raise a StopIteration. There's no reason for doing
> that, other than taking dirty implementation shortcuts and ending up with
> difficult to maintain code.

I'm inclined to agree. This code seems to be designed
to cause the LC to exit early, which IMO is an abuse
of LCs. If you need a loop that doesn't run to
completion, you should write it out as a for-loop.

So no, this is not a bug, since in well-designed code
the difference in behaviour shouldn't matter.

-- 
Greg


More information about the Python-3000 mailing list