hash codes in python the standard mechanism for computing hash codes in python is built-in function with signature hash(xthat returns an integer value that serves as the hash code for object howeveronly immutable data types are deemed hashable in python this restriction is meant to ensure that particular object' hash code remains constant during that object' lifespan this is an important property for an object' use as key in hash table problem could occur if key were inserted into the hash tableyet later search were performed for that key based on different hash code than that which it had when insertedthe wrong bucket would be searched among python' built-in data typesthe immutable intfloatstrtupleand frozenset classes produce robust hash codesvia the hash functionusing techniques similar to those discussed earlier in this section hash codes for character strings are well crafted based on technique similar to polynomial hash codesexcept using exclusive-or computations rather than additions if we repeat the experiment described in table using python' built-in hash codeswe find that only strings out of the set of more than , collide with another hash codes for tuples are computed with similar technique based upon combination of the hash codes of the individual elements of the tuple when hashing frozensetthe order of the elements should be irrelevantand so natural option is to compute the exclusive-or of the individual hash codes without any shifting if hash(xis called for an instance of mutable typesuch as lista typeerror is raised instances of user-defined classes are treated as unhashable by defaultwith typeerror raised by the hash function howevera function that computes hash codes can be implemented in the form of special method named hash within class the returned hash code should reflect the immutable attributes of an instance it is common to return hash code that is itself based on the computed hash of the combination of such attributes for examplea color class that maintains three numeric redgreenand blue components might implement the method asdef hash (self)return hash(self redself greenself bluehash combined tuple an important rule to obey is that if class defines equivalence through eq then any implementation of hash must be consistentin that if =ythen hash( =hash(ythis is important because if two instances are considered to be equivalent and one is used as key in hash tablea search for the second instance should result in the discovery of the first it is therefore important that the hash code for the second match the hash code for the firstso that the proper bucket is examined this rule extends to any well-defined comparisons between objects of different classes for examplesince python treats the expression = as trueit ensures that hash( and hash( are the same