Getting a set of lambda functions

Ivan Illarionov ivan.illarionov at gmail.com
Sun May 25 08:14:25 EDT 2008


On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote:

> Hi,
> 
> I try to get a set of lambda functions that allows me executing each
> function code exactly once. Therefore, I would like to modify the set
> function to compare the func_code properties (or the lambda functions to
> use this property for comparison).
> 
> (The reason is that the real function list is quite large (> 1E5), there
> are only few functions with non-equal code and the order of execution is
> not important.)
> 
> How can I achieve this?
> 
>>>> func_strings=['x', 'x+1', 'x+2', 'x'] funclist = [eval('lambda x:' +
>>>> func) for func in func_strings] len(funclist)
> 4
>>>> len(set(funclist))
> 4
>>>> funclist[0].func_code == funclist[3].func_code
> True
>>>> funclist[0] == funclist[3]
> False
> 
> 
> Thanks in advance
> 
> Martin

Maybe make a set of code objects?

func_code_set = set([f.func_code for f in funclist])

funclist = []
for fc in func_code_set:
    f = lambda x: x
    f.func_code = fc
    funclist.append(f)

-- Ivan



More information about the Python-list mailing list