↓ Skip to main content
  1. Posts/

Immutable Dictionaries With Frozendict In Python 3.15

··182 words·1 min·

🐍 Python 3.15 adds frozendict, a dictionary that does not allow items to be added, changed, or removed. It is useful for lookup tables or data that should stay constant: it communicates that intent and helps prevent accidental changes.

It can also be useful when loading JSON that represents immutable information. And if all its values are hashable, a frozendict can be used as another dictionary’s key. One caveat: it is not deeply immutable; if a value is a list, that list can still be changed.

The author recommends using Mapping in type annotations when a function only needs to read a mapping. That lets it accept both dict and frozendict without requiring a specific implementation.

💡 Explanation in a nutshell
#

A dict is like a whiteboard you can edit. A frozendict is more like a read-only version: it keeps the same key-value associations but prevents the structure from being changed after creation.

More information at the link 👇

Also published on LinkedIn.

Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano