Documentation of __hash__

Stefan Halfpap klauck2 at gmail.com
Wed Feb 5 08:57:10 EST 2020


Hi,

I do not understand the following statement from the python (2 and 3) documentation regarding __hash__ and __eq__ methods:
"If a class does not define an __eq__() <https://docs.python.org/3/reference/datamodel.html#object.__eq__> method it should not define a __hash__() <https://docs.python.org/3/reference/datamodel.html#object.__hash__> operation either;”
(see https://docs.python.org/3/reference/datamodel.html#object.__hash__ <https://docs.python.org/3/reference/datamodel.html#object.__hash__> )

I thought it relates to the second part (“if it defines __eq__() <https://docs.python.org/3/reference/datamodel.html#object.__eq__> but not __hash__() <https://docs.python.org/3/reference/datamodel.html#object.__hash__>, its instances will not be usable as items in hashable collections”), which is totally clear to me.
But then the implication should be the other way around.

But what is the reason (meaning) for the statement as it is?

Best regards,
Stefan


More information about the Python-list mailing list