Documentation of __hash__

Christian Gollwitzer auriocus at gmx.de
Wed Feb 5 16:19:53 EST 2020


Am 05.02.20 um 20:55 schrieb klauck2 at gmail.com:

> If not, why should a class not define __hash__, if it does not define __eq__?

Hashes are not unique. When you insert or look up something in a 
hashtable, the hash is computed and used as the index into the table. 
Because the hash is not necessarily unique, if hash(a)=hash(b), it can 
still be that a=/= b. Therefore, in a second step, a is compared to b if 
the hashes match.

Therefore, you need a comparison operator which is compatible with the 
hash function, i.e. if a==b, then hash(a) must be equal to hash(b).

Christian



More information about the Python-list mailing list