Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

Python 3.15 Has a New Kind of Dict. And It’s Not What I Expected.

--

Your dictionaries can now be hashable, cacheable, and impossible to accidentally modify. Here are 7 surprisingly useful things that unlocks.

Press enter or click to view image in full size
The new frozen dict in Python.

Python dictionaries are everywhere. We use them for configuration, API responses, experiment parameters, lookup tables, and countless other everyday tasks.

They are flexible, convenient, and easy to work with. But sometimes that flexibility becomes a limitation.

What if you want to use an entire dictionary as a key? What if you want to cache a function that accepts a configuration dictionary? Or make sure a function cannot accidentally modify settings shared across your application?

Until recently, these situations required workarounds or additional libraries.

Python 3.15 introduces frozendict, a new built-in immutable dictionary type.

It behaves much like a regular dictionary, with one important difference: once created, its entries cannot be changed.

config = frozendict(batch_size=64, epochs=20)
print(config["batch_size"]) # 64

config["batch_size"] = 128 # TypeError

There is no package to install and no special import. frozendict is part of Python itself.

--

--

Bernd Fischer
Bernd Fischer

Written by Bernd Fischer

Python and Rust Developer, IT-Trainer, Coach, Geek, RPGs, Pizza, Traveller.

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.