Getting a set of lambda functions

Martin Manns mmanns at gmx.net
Sun May 25 19:55:03 EDT 2008


On Sun, 25 May 2008 14:39:28 -0700 (PDT)
bearophileHUGS at lycos.com wrote:

> This may have some bugs left, but it looks a bit better:
[...]
>         self._hash = hash(self._func.func_code) ^ \
>                      hash(tuple(signature[0]) + tuple(signature[1:3]))
>     def __eq__(self, other):
>         return self._func.func_code == other._func.func_code and \
>                getargspec(self._func) == getargspec(other._func)
>     def __hash__(self):
>         return self._hash
>     def __call__(self, *args, **kwargs):
>         return self._func(*args, **kwargs)
> 
> I haven't put a full hashing of getargspec(self._func) too into the
> __init__() because it may contain too much nested unhashable
> structures, but the __eq__() is able to tell them apart anyway, with
> some collisions.

Looking at a function:

>>> a=lambda x:x
>>> dir(a)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']

Should func_globals and func_name also be taken into account for
__eq__()?

Best Regards

Martin



More information about the Python-list mailing list