参考文献:
python-对象之hashable&unhashable与immutable&mutable
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 (except with themselves), and their hash value is derived from their id()
参考文献:
Python - glossary
All the answers here have good working explanation of hashable objects in python, but I believe one needs to understand the term Hashing first.
Hashing is a concept in computer science which is used to create high performance, pseudo random access data structures where large amount of data is to be stored and accessed quickly.
For example, if you have 10,000 phone numbers, and you want to store them in an array (which is a sequential data structure that stores data in contiguous memory locations, and provides random access), but you might not have the required amount of contiguous memory locations.
So, you can instead use an array of size 100, and use a hash function to map a set of values to same indices, and these values can be stored in a linked list. This provides a performance similar to an array.
Now, a hash function can be as simple as dividing the number with the size of the array and taking the remainder as the index.
For more detail refer to https://en.wikipedia.org/wiki/Hash_function
Here is another good reference: http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html
参考文献:
What do you mean by hashable in Python?
def hash(self):
return hash((self.name, self.nick, self.color))
If a class does not define a cmp() or eq() method it should not define a hash() operation either; if it defines cmp() or eq() but not hash(), its instances will not be usable in hashed collections. If a class defines mutable objects and implements a cmp() or eq() method, it should not implement hash(), since hashable collection implementations require that an object’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).
User-defined classes have cmp() and hash() methods by default; with them, all objects compare unequal (except with themselves) and x.hash() returns a result derived from id(x).
Classes which inherit a hash() method from a parent class but change the meaning of cmp() or eq() such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting hash = None in the class definition. Doing so means that not only will instances of the class raise an appropriate TypeError when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable) (unlike classes which define their own hash() to explicitly raise TypeError).
Changed in version 2.5: hash() may now also return a long integer object; the 32-bit integer is then derived from the hash of that object.
Changed in version 2.6: hash may now be set to None to explicitly flag instances of a class as unhashable.
参考文献:
hash - Python reference