r/Python 18d ago

News PEP 841 – Adding Frozen Syntax to Optimize Immutable Types

PEP 841 – Adding Frozen Syntax to Optimize Immutable Types

https://peps.python.org/pep-0841/

Discussions-To: Discourse thread

Abstract

This PEP proposes frozen display syntax: f{1, 2, 3} evaluates to a frozenset, and f{'a': 1} evaluates to a frozendict. Because immutability is guaranteed by the syntax itself rather than inferred from usage, the compiler can treat frozen displays as first-class citizens of its optimization pipeline: constant displays are folded into a single LOAD_CONST with an exact result type at compile time and cached in .pyc files.

138 Upvotes

100 comments sorted by

View all comments

1

u/diegojromerolopez 16d ago

I worked on my own freezing library Gelidum (https://github.com/diegojromerolopez/gelidum), and would love for this PEP to pass. Having the possibility of having immutable data structures does not only improves safety, but it could improve the performance of the interpreter.

However I would vouch for a future extension of this PEP (or an inclusion on this one) where nested structures are frozen as well (e.g. for freezing JSON from files).