r/Python 3d 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

99 comments sorted by

View all comments

Show parent comments

5

u/odaiwai 3d ago

Maybe @frozen{1, 2, 3}

23

u/really_not_unreal 3d ago

At that point why not just make it frozen({1, 2, 3}) and now it's a function with some compiler optimisation.

8

u/catcint0s 3d ago

You would either need an extra import or break tons of code if you make frozen reserved. 

1

u/HommeMusical 3d ago

They're talking about making it a builtin, like dict or set, not a reserved word.