removing list comprehensions in Python 3.0

Steven Bethard steven.bethard at
Sun Jul 10 06:32:22 CEST 2005

Raymond Hettinger wrote:
> [Steven Bethard]
>>I would hope that in Python 3.0 list comprehensions and generator
>>expressions would be able to share a large amount of implementation, and
>>thus that the speed differences would be much smaller.  But maybe not...
> Looking under the hood, you would see that the implementations are
> necessarily as different as night and day.  Only the API is similar.

Necessarily?  It seems like list comprehensions *could* be implemented 
as a generator expression passed to the list constructor.  They're not 
now, and at the moment, changing them to work this way seems like a bad 
idea because list comprehensions would take a performance hit.  But I 
don't understand why the implementations are *necessarily* different. 
Could you explain?


P.S. The dis.dis output for list comprehensions makes what they're doing 
pretty clear.  But dis.dis doesn't seem to give me as much information 
when looking at a generator expression:

py> def ge(items):
...     return (item for item in items if item)
py> dis.dis(ge)
   2           0 LOAD_CONST               1 (<code object <generator 
expression> at 0116FD20, file "<interactive input>", line 2>)
               3 MAKE_FUNCTION            0
               6 LOAD_FAST                0 (items)
               9 GET_ITER
              10 CALL_FUNCTION            1
              13 RETURN_VALUE

I tried to grep through the dist\src directories for what a generator 
expression code object looks like, but without any luck.  Any chance you 
could point me in the right direction?

More information about the Python-list mailing list