Special Method Protocols

__hash__() — object lifecycle protocol

Supply an integer hash for hash() and hashed collections. Equal objects must have equal hashes; avoid hashing mutable equality state. Defining __eq__ without __hash__ normally makes instances unhashable.

Formal Syntax & Structure

python syntaxpython
def __hash__(self):

Return Value

int

Hash integer, normalized by the interpreter.

Working Example

Example Snippetpython
class Key:    def __init__(self, value): self.value = value    def __eq__(self, other): return isinstance(other, Key) and self.value == other.value    def __hash__(self): return hash(self.value)print({Key(7): "found"}[Key(7)])
Python Reference