osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Documentation of __hash__


Stefan Halfpap wrote at 2020-2-5 14:57 +0100:
>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.

"if not A then not B" is equivalent to "if B then A".

In your case: "__eq__ not defined, then __hash__ not defined"
is equivalent to "__hash__ definied requires __eq__ defined".