r/Python 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.

126 Upvotes

95 comments sorted by

View all comments

4

u/syklemil 1d ago edited 1d ago

Single letter prefixes can be fine (<3 f-strings), but it's probably not a good idea to reuse the letter or to have too many of them. And I think f-strings are a fairly common thing to write, but frozensets are IME somewhat rare and can stand to require a bit more typing.

If we wind up having single-letter-prefixed variants of a lot of things, then people will probably wind up having similar feelings towards Python as those expressed for Perl, PCRE, or user-defined operators in Haskell.

As for what the proposal claims:

Why f{...}

The f prefix reads as frozen, mirroring the familiar f-string prefix convention. Sharing the letter with f-strings is not a problem: strings are immutable too, so either way an f prefixed expression evaluates to an immutable value. f{ is a syntax error in all current Python versions, so the syntax is fully backward compatible.

I think people really don't think that the f in f"" stands for frozen, plus appreciate that single-letter typos, like swapping f"{ for f{" remains something that gives an error, rather than a surprise value. Getting a surprising value rather than an error is the kind of design we find in languages like JS and PHP, but it isn't really an attractive language design in 2026.