Python 2.0b1 List comprehensions are slow

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Mon Sep 11 15:49:21 CEST 2000


Duncan Booth wrote in comp.lang.python:
> How would you convert this one to a lambda?
> 
> >>> def test(fn):
> ...     a, b, c = 1, 2, 3
> ...     list = [fn()[z] for z in ('a', 'b', 'c')]
> ...     print list
> ...
> >>> test(locals)
> [1, 2, 3]

Heh. That's dirty. I was thinking about

def test(fn):
   a,b,c = 1,2,3
   list = map(lambda x,fnres=fn(): fnres[x], 'abc')
   print list
   
But it doesn't work if fn() has a side effect. fn() is only called once, which
is usually good but may be wrong :-). 

The lambda could be replaced by "fn().get", that's somewhat less general,
but faster.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
Signature server down. Manually insert witty comment here.



More information about the Python-list mailing list