TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.8K subscribers
Post #9195 1.38K
In Python 3.15, there will be a fully immutable dictionary.

A new public immutable type, frozendict, is added to the builtins module.

It is expected that frozendict will be "safe by design", because it prevents any unintended changes. This is useful not only for the CPython standard library, but also for third-party maintainers: you can rely on a reliable immutable dictionary type.

Why is this needed at all:

▪️Do you want to use a map as a key in another dict or put it in a set? A regular dict is not allowed, but a frozendict is (if the values are also hashable).
▪️ @functools.lru_cache() and arguments-dictionaries: it's difficult with a dict, but normal with a frozendict.
▪️Defaults in function arguments: instead of a "mutable default", you can give frozendict(...) and not get surprises.

How it looks in the API:

▪️The constructor "like a dict": frozendict(), frozendict(**kwargs), frozendict(mapping) or iterable pairs, plus you can mix with **kwargs.
▪️The order of insertion is preserved (as in a regular dict).
▪️The hash does not depend on the order of elements (logic via frozenset(items)), and the comparison is also based on the content, not on the order.
▪️There is a union via | and an "update" |= (but |= does not mutate the object, but creates a new one).
▪️.copy() in CPython essentially returns the same object (shallow), and if you need deep copying, then copy.deepcopy().
An important point: frozendict is NOT inherited from dict. This is done on purpose, so that you can't bypass the "immutability" by calling dict.__setitem__ and similar tricks.

And a bonus for the stdlib: the authors have marked places where you can replace constant/public maps with frozendict (including where MappingProxyType is now used).

👉 @DataScience4
Python Enhancement Proposals (PEPs) PEP 814 – Add frozendict built-in type | peps.python.org A new public immutable type frozendict is added to the builtins module.
  • ❤ 4
More from @pythonre
  1. Oct 14, 2026Build AI Agents with Google ADK: Gemini, Memory & Vertex AI Build production-ready AI agen…
  2. Oct 13, 2026https://substack.com/@husseinsheikho?utm_source=share&utm_medium=android&r=3hwnr8 Follow m…
  3. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  4. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  5. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  6. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →