[Tutor] Is there a test for hashability?
Richard D. Moores
rdmoores at gmail.com
Thu Sep 1 16:32:01 CEST 2011
The glossary defines "hashable" as:
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__() 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().
I'm trying to write a general test for hashability. How can I test if
an object has both a __hash__() method and an __eq__() method?
More information about the Tutor