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.
In more detail
Immutable built-ins such as int, str and tuples of hashable values are hashable. Mutable containers such as list, dict and set are unhashable, and trying to use one as a key raises TypeError. A class that defines __eq__ without __hash__ becomes unhashable by default.
Related terms
- hash-equals contract — The rule that if two objects are equal, a == b, then their hashes must be equal too, hash(a) == hash(b).
- immutable — Something that cannot be changed after it is created; to change it you make a new one.
- mutable — Able to be changed in place after it is created, like most lists, arrays and objects in languages such as Python and JavaScript.
- TypeError — The Python exception raised when an operation gets a value of the wrong kind, such as hashing a list.
- dictionary — A Python collection that stores values under keys, written in curly brackets, such as {"tea": 250}.
- set (Python) — An unordered collection of distinct values, written {1, 2, 3} or made with set(), where each value appears only once.
- __hash__ — The dunder method that returns an object's hash value, which lets it be a dictionary key or a set member.
Taught in
- Python Dicts And Sets Under The Hood — Algorithms & Data Structures for Working Engineers
- Lists, Tuples, Sets, Dicts: Choosing One — Hands-on Python: Complete Video Course & Book
- Dunder Methods, And Making Objects Pythonic — Hands-on Python: Complete Video Course & Book
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep