From the Python Glossary:
An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__()
method), and can be compared to other objects (it needs an __eq__()
or __cmp__()
method). Hashable objects which compare equal must have the same hash value.
Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().
Dicts and sets must use a hash for efficient lookup in a hash table; the values must also be immutable, because you can't change the hash once it's put in the table. That's why you often see these requirements mentioned together, because neither one is sufficient and both must be met.
Edit: Reading closely, I see that I glossed over something significant: An object is hashable if it has a hash value which never changes during its lifetime. So by the official definition, anything mutable can't be hashable, even if it has a __hash__()
function. My statement about both requirements being necessary is untrue, because being hashable already implies the requirement to be immutable.