hash-equals contract

The rule that if two objects are equal, a == b, then their hashes must be equal too, hash(a) == hash(b).

In more detail

A hash table first uses the hash to find a slot and then uses equality to confirm the key, so equal objects with different hashes would never be found. The rule only runs one way: unequal objects may share a hash, which is just a collision. In Python, define __hash__ from the same fields that __eq__ compares, and only use immutable fields.

Related terms

Taught in

References