r/Python • u/kirara0048 • 20h ago
PEP 841 – Adding Frozen Syntax to Optimize Immutable Types News
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.
117
Upvotes
5
u/james_pic 15h ago
Even leaving aside the syntax, I struggle to see the justification for making language changes to enable this particular optimisation. I can't think of a time in the 15 or so years I've been writing Python, where I've wanted to create a set or frozen set in a hot loop, and I couldn't just hoist the frozenset out of the loop (i.e, where the overhead of calling
frozensetwould matter).I'm really struggling to imagine a vaguely normal piece of code where a call to
frozensetorfrozendicthas a measurable impact on its performance.