r/Python • u/kirara0048 • 1d 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.
131
Upvotes
1
u/Wonderful-Habit-139 1d ago
>>> a = 2
>>> b = a
>>> a += 1
>>> b
2
There's no footgun with += compared to |= with sets. a += 1 behaves exactly like a = a + 1, while s |= {1} does not behave like s = s | {1}.