__hash__
The dunder method that returns an object's hash value, which lets it be a dictionary key or a set member.
In more detail
It must be consistent with __eq__: equal objects need equal hashes, and the hash must not change while the object is in a set or dictionary, so hash only fields that never change. Dataclasses can generate a matching pair for you with frozen=True.
Related terms
- __eq__ — The dunder method that defines what == means for your objects; without it, two instances are equal only if they are the same object.
- hashable — Describes a Python object that has a hash value that never changes during its lifetime and can be compared for equality, so it can be a dict key or set member.
- dunder method — A method whose name starts and ends with two underscores, such as __init__ or __len__, which Python calls for you to implement built-in behaviour.
Taught in
- Dunder Methods, And Making Objects Pythonic — Hands-on Python: Complete Video Course & Book