Loading section...
Frozensets: Immutable Sets
Concepts: pyFrozensets
A frozenset is an immutable version of a set. Once created, a frozenset cannot be modified: you cannot add or remove elements. This immutability might seem like a limitation, but it has an important consequence that unlocks powerful patterns: frozensets are hashable, meaning they can be used as dictionary keys or as elements of other sets. To understand why immutability enables hashing, recall that hash tables (the underlying data structure for both dictionaries and sets) rely on objects having stable hash values. If you could change a set after using it as a dictionary key, its hash value would change, and the dictionary would no longer be able to find it. Python prevents this by making regular sets unhashable. Frozensets solve this by guaranteeing that the content never changes. Frozense