-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Closed as not planned
Closed as not planned
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
For functools.partial
and operator.{attrgetter,itemgetter,methodcaller}
objects, instances are compared with their id
s 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
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement