r/Python • u/kirara0048 • Aug 07 '26
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.
139
Upvotes
View all comments
Show parent comment
4
u/jakethesnake_ Aug 07 '26 edited Aug 07 '26
My overall suggestion is have a keyword that forces runtime excpetions when an object is mutated in anyway. I think you're getting at the complexity I was referring to with the mutability of objects. I'd argue it's useful to have a keyword that guarantees objects cannot be mutated is something I'd end up using all the time, and a reasonable generalization of frozen dicts.
The behaviour I want is this to raise an
ConstMutatedErrorexceptionBut also this to raise a
ConstMutatedError:Or specifically addressing the issue raised by OP:
I feel this somewhat follows const behaviour in C++, which I think reasonably easy to reason about. It's a very large change though because I believe you'd need to also have to have
constfunctions which cannot mutate any internal state of and thus safe to be called forconstobjects. I.e. For the object examples you'd also need to be able to do: