Hashable Objects
An object is hashable if it has a hash code 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 code.
Hashable Types
Numeric
types;- Flat immutable1 types
str
andbytes
; - Container types1 if the object and all elements are immutable and hashable:
Characteristics
- The
hash code might vary
based on the Python version, machine setup, and the way the hash was created; - The hase code is
guaranteed to be the same only within one Python process
; Custom user-defined types are hashable by default
. This happens because their hash code is derived from theirid()
, and the__eq__()
method, which is inherited from the object class, simply checks whether the object IDs are the same;- If an
object implements a custom __eq__()
that takes into account itsinternal state
, it will be hashable only if its__hash__() always returns the same hash code
:- In practice, this requires that
__eq__()
and__hash__()
only take into account instance attributes that never change during the life of the object
- In practice, this requires that
Code
hashable_obj.py
>>> tt = (1, 2, (30, 40))
>>> print(hash(tt))
>>> tl = (1, 2, [30, 40])
>>> try:
... hash(tl)
... except BaseException as ex:
... print(f"Hash error when use unhashable obj: {ex}")
>>> tf = (1, 2, frozenset([30, 40]))
>>> print(hash(tf))
-3907003130834322577
Hash error when use unhashable obj: unhashable type: 'list'
5149391500123939311