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

Guido van Rossum guido at python.org
Wed Jul 9 18:22:37 CEST 2008


It's suspect, please file a bug. Thanks for noticing!

On Wed, Jul 9, 2008 at 7:49 AM, Carl Johnson <carl at carlsensei.com> wrote:
> Python 3.0a5 (r30a5:62856, May 10 2008, 10:34:28)
> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def f(x):
> ...  if x > 5: raise StopIteration
> ...
>>>> [x for x in range(100) if not f(x)]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "<stdin>", line 1, in <listcomp>
>  File "<stdin>", line 2, in f
> StopIteration
>>>> list(x for x in range(100) if not f(x))
> [0, 1, 2, 3, 4, 5]
>
> Certainly, it's an inconsistency compared to generator expressions, but is
> it a bug? I know that Python 3000 is making it so that list comprehension
> variables don't leak anymore, so I take it that the goal is to have them be
> more similar to generator expressions than not. Then again, maybe there are
> good reasons for this behavior? My own feeling is that these two cases
> should behave the same way, but I could be wrong.
>
> I hope this isn't an old issue, but I've been lurking for a few months and
> haven't heard any discussion of this.
>
> -- Carl Johnson
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe:
> http://mail.python.org/mailman/options/python-3000/guido%40python.org
>



-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list