Python 2.0b1 List comprehensions are slow
Duncan Booth
duncan at rcp.co.uk
Mon Sep 11 06:59:33 EDT 2000
aleaxit at yahoo.com (Alex Martelli) wrote in
<8pi88101h3d at news2.newsguy.com>:
>"Skip Montanaro" <skip at mojam.com> wrote in message
>news:14778.60326.721002.701285 at beluga.mojam.com...
>> 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.
>
>Would it...? The codeobject corresponding to the lambda does seem to
>have all of this info at its disposal...:
>
>>>> lam=lambda x,y,z=12: x+y+z+t+u
>>>> lam.func_code.co_varnames
>('x', 'y', 'z')
>>>> lam.func_code.co_names
>('x', 'y', 'z', 't', 'u')
>>>>
>
>The codeobject doesn't know about default values (which local variables
>are them, and what those values are), but it does seem to know about
>non-local identifiers referenced in the code (those in co_names that
>are not also in co_varnames). What am I missing...?
>
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]
More information about the Python-list
mailing list