This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: improve documentation of __hash__
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder: confusing docs with regard to __hash__
View: 14617
Assigned To: docs@python Nosy List: docs@python, max, r.david.murray
Priority: normal Keywords:

Created on 2012-09-20 11:00 by max, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg170798 - (view) Author: Max (max) * Date: 2012-09-20 11:00
In dev/reference/datamodel#object.__hash__, there are two paragraphs that seem inconsistent. The first paragraph seems to say that a class that overrides __eq__() *should* explicitly flag itself as unhashable. The next paragraph says that a class that overrides __eq__() *will be* flagged unhashable by default. Which one is it?

Here are the two paragraphs:

Classes which inherit a __hash__() method from a parent class but change the meaning of __eq__() such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting __hash__ = None in the class definition. Doing so means that not only will instances of the class raise an appropriate TypeError when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable) (unlike classes which define their own __hash__() to explicitly raise TypeError).

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__. Otherwise the inheritance of __hash__() will be blocked, just as if __hash__ had been explicitly set to None.
msg170816 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-09-20 14:59
This has already been fixed, and the change is visible in the online documentation.
History
Date User Action Args
2022-04-11 14:57:36adminsetgithub: 60185
2012-09-20 14:59:39r.david.murraysetstatus: open -> closed

superseder: confusing docs with regard to __hash__

nosy: + r.david.murray
messages: + msg170816
resolution: duplicate
stage: resolved
2012-09-20 11:00:23maxcreate