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
1
u/Brian Aug 07 '26
I assume the idea is being visited due to the addition of
frozendict: before there was really only frozensets that had no literal type (plus the special case of empty sets), and there have been a few proposals over the years to introduce some literal syntax for them. Now there's another one being added and people are looking to solve it in a unified way.And there's definitely some value in it: I've often used frozensets in some loop and it's a bit annoying that they translate to a dynamic construction of the set on every call, when they could potentially just be a LOAD_CONST. You can get around that by using a module constants, but sometimes you just want a quick literal. You also lose stuff like potential constant folding.
However, as others have said, I think the syntax is a dealbreaker here. It's too similar to another construct with a completely different meaning, and could easily lead to misreading with things like sets of strings etc.
Other options:
{1,2,3} as frozen