Skip to content

Add meaningful __eq__ method for functools.partial and operator.{attrgetter,itemgetter,methodcaller} #130112

@XuehaiPan

Description

@XuehaiPan

Feature or enhancement

Proposal:

For functools.partial and operator.{attrgetter,itemgetter,methodcaller} objects, instances are compared with their ids rather than the behavior of the callable. This is not intuitive. I ran into this feature request due to an unexpected result when reconstructing pickled callables.

In [1]: import functools

In [2]: functools.partial(print) == functools.partial(print)
Out[2]: False

In [3]: import operator

In [4]: operator.methodcaller('some_method') == operator.methodcaller('some_method')
Out[4]: False

In [5]: import pickle

In [6]: f = functools.partial(print)

In [7]: pickle.loads(pickle.dumps(f)) == f
Out[7]: False

In [8]: g = operator.methodcaller('some_method')

In [9]: pickle.loads(pickle.dumps(g)) == g
Out[9]: False

It would be nice to make the higher-level callable equals if the arguments are the same.

Currently, the hash is calculated via object.__hash__ based on the memory address. One potential problem is that we will need to also update __hash__ if we update __eq__. The user arguments might not be hashable (e.g. values in args, kwargs for functools.partial).

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions