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

Carl Johnson carlmj at hawaii.edu
Wed Jul 9 16:46:18 CEST 2008

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
 >>> 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

More information about the Python-3000 mailing list