Python 2.0b1 List comprehensions are slow

Skip Montanaro skip at mojam.com
Sat Sep 9 22:02:14 EDT 2000


    Robin> seems to me that this kind of code could be optimised by using
    Robin> the map idiom. To me
    Robin>         [expr for x in L] seems to correspond to 
    Robin>         map(lambda vars: expr,L)

Yes, except that all expressions in list comprehensions have access to the
current local scope.  Anything pushed into a lambda would lose that scope.
Generation of the lambda would have to know the details of the local and
global variables referenced in the expression and build the lambda
accordingly.  I think that would require more information than the current
code generator has at its disposal.

(thinking out loud... on the other hand, could you maybe generate a special
anonymous function that refers to the locals of the function in which it was
defined?)

-- 
Skip Montanaro (skip at mojam.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list