Title: Equality and hashing for functools.partial
Type: enhancement Stage:
Components: Extension Modules Versions: Python 3.2
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: daniel.urban, rhettinger
Priority: normal Keywords: patch

Created on 2010-05-12 15:00 by daniel.urban, last changed 2010-09-10 18:14 by rhettinger. This issue is now closed.

File name Uploaded Description Edit
partial_eq_hash.diff daniel.urban, 2010-05-12 15:00
partial_eq_hash_2.diff daniel.urban, 2010-05-13 07:24 Patch 2 (py3k branch)
partial_eq_hash_3.diff daniel.urban, 2010-05-13 16:39 Patch 3 (py3k branch)
partial_eq_hash_4.diff daniel.urban, 2010-05-16 12:20 Patch 4 (py3k branch)
Messages (6)
msg105591 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2010-05-12 15:00
On python-dev came up an idea [1] to support equality (== and !=) and hashing by functools.partial instances.  Van Lindberg provided an implementation written in Python [2].  I've made a very similar implementation in C (in Modules/_functoolsmodule.c).  The Python equivalent of my code is in Lib/test/ as the PythonPartialCls class.  The hashing differs a little from Van Lindberg's implementation: I'm computing the "normal form" of the dict as the sorted list of its items (not as the sorted list of the keys followed by the items).  (It was easier to implement this way.)

I haven't made a lot of Python programming in C, so I'm not sure I made everything in the right way (especially the reference counting).  Anyway, I'm attaching my patch.  I'd appreciate every suggestion, and will try to correct my mistakes.

msg105625 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2010-05-13 07:24
Sorry, I realized I made a stupid mistake.  (I didn't use PyList_Sort to sort the list in partial_hash.)
Here is the corrected patch.
msg105645 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2010-05-13 16:39
On python-dev Yaniv Aknin pointed out that the keywords dictionary of a partial object is mutable [1].  This causes problems with hashing and equality.  The new patch replaces the keywords dictionary with a read-only proxy of that dictionary.  This seems to solve the problem. 

msg105861 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2010-05-16 12:20
In this new patch, __eq__ compares also the __dict__ of the two partial instances.
msg116019 - (view) Author: Daniel Urban (daniel.urban) * (Python triager) Date: 2010-09-10 13:35
Is there anybody, who is interested in this?
There are some substantial problems with it (hashing a dict), so I suggest that this should be closed, unless somebody is interested.
msg116033 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-09-10 18:14
Thanks for the interesting thread.
I do agree with your suggestion to close this one though.
Date User Action Args
2010-09-10 18:14:47rhettingersetstatus: pending -> closed

nosy: + rhettinger
messages: + msg116033

resolution: rejected
2010-09-10 13:35:59daniel.urbansetstatus: open -> pending

messages: + msg116019
2010-05-16 12:20:21daniel.urbansetfiles: + partial_eq_hash_4.diff

messages: + msg105861
2010-05-13 16:39:48daniel.urbansetfiles: + partial_eq_hash_3.diff

messages: + msg105645
2010-05-13 07:24:38daniel.urbansetfiles: + partial_eq_hash_2.diff

messages: + msg105625
2010-05-12 15:00:47daniel.urbancreate