PEP 841: Adding Frozen Syntax to Optimize Immutable Types
The proposal introduces new 'f' prefix syntax for literal collections, specifically f{...} for frozensets and f{'key': value} for frozendicts. This syntax enables the Python compiler to perform constant folding and caching of these immutable structures directly into .pyc files.
Verified State Diff
Impact & Verification Analysis
Python core developers, library maintainers, and performance-sensitive application developers.
It reduces runtime overhead for immutable data structures and improves startup performance by allowing complex immutable objects to be pre-computed and cached during the compilation phase.
Full Fact Overview
PEP 841 shifts the responsibility of immutability from runtime inference to compile-time syntax. By introducing dedicated frozen display syntax, the Python interpreter can bypass the overhead of constructing these objects at runtime. This allows the compiler to treat these structures as constants, facilitating optimization via LOAD_CONST and reducing memory allocation overhead for immutable collections in hot code paths.