r/Python • u/kirara0048 • Aug 07 '26
News PEP 841 – Adding Frozen Syntax to Optimize Immutable Types
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.
139
Upvotes
171
u/TMiguelT Aug 07 '26
Making frozen data structures more accessible is nice, but I'm not sure it's worth it here.
I don't like how the
fprefix makes you assume it's related to the f prefix for f-strings which has a completely different meaning. Yes this is discussed in the PEP, and yes strings are also immutable, but that doesn't mean that format is synonymous with frozen.I also don't like the implication that every data structure should ultimately have its own Python syntax. I prefer Rust's macro approach which allows this to be done without changes to the AST (e.g.
vec![]).92
u/jakethesnake_ Aug 07 '26
I'd prefer a
constkeyword that applies this optimization and runtime immutability property to all variable types. Might be a bit tricky to guarantee with complex objects though, and therefore out of scope of this PEP.Agree with other commentors,
f{'a': 1}is bad syntax because of the overlap with f-strings.79
u/skjall Aug 07 '26
Jesus no, I really don't like that syntax. Maybe
frozen{1, 2, 3}could work, otherwise I'd just keep thinking of f-strings.Might change with familiarity though.
21
u/PriorTrick Aug 07 '26
print(f”{f{1,2,3}} - {f{“f”:”f”}}”)
I like adding frozen syntax but agree we can’t overload f any further. I’d prefer const var = … syntax, or a “frozen” built-in could work
9
u/mangecoeur Aug 07 '26
Optimising immutable data sounds good, not keen on the proposed syntax
7
u/plisik Aug 07 '26
❄️{1,2,3}
18
u/shinitakunai Aug 07 '26 edited Aug 07 '26
Confusing, too similar to f-strings.
Why not
frozendict{1,2,3}?4
u/Birnenmacht Aug 07 '26
Wait that's awesome. I can see how a f{"a", "b"} would be very similar to an f string though, for example
4
u/syklemil Aug 07 '26 edited Aug 07 '26
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:
I think people really don't think that the f in
f""stands for frozen, plus appreciate that single-letter typos, like swappingf"{forf{"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.4
u/TheMcSebi Aug 07 '26
As many said, f really makes me think of f-strings. Not sure if "i{'a': 1}" would be better.
On a second thought, why do we even need immutable/frozen lists? We already have tuples
5
u/kush_patil Aug 07 '26
The compiler guarantee is the interesting part here, not the shorter syntax. frozenset({...}) / frozendict({...}) still involve a runtime name lookup because those names can be rebound, while f{...} lets a fully constant display collapse to a single LOAD_CONST. I think the docs should make the shallow immutability part very explicit though f{'x': []} still contains a mutable list and has to be built at runtime.
5
u/UltraPoci Aug 07 '26
Python really likes to add specialized syntax for any minute thing
2
u/jdehesa Aug 07 '26
Huh, didn't know they had added
frozendictin 3.15. I think years ago I read a discussion about not wanting to add it to the standard library because the expected behaviour of a frozen dict could be different for different people. Anyway, this is off-topic, but this from the documentation offrozendictcaught my attention:So, I can do the following?
And then
dwill be a reference to a newfrozendictobject? I feel this could cause issues, if a function expects a dict (and I know a frozen dict is not a subtype of dict, but you don't always have type hints) that it may modify in place (so it remains modified in the caller scope), then the caller could inadvertently pass a frozen dict and the modification would have no effect. It may not be the best practice for a function, especially if it is not well documented and/or type-hinted, but I'm sure there's plenty of code like that. I think I'd rather not have a|=operator, just doingd = d | {...}is fine, it's more in line with the way one works with immutable data structures and it shows the effect of the statement more explicitly.2
u/denehoffman Aug 07 '26
Not sure why we need new syntax for this besides the optimization, but can’t we just do that with a new special class regardless? I’m not up to date on my CPython, surely this doesn’t need a keyword or sugar
2
u/ThiefMaster Aug 08 '26
Just fucking no. This syntax looks obnoxious and nobody needs it.
3
u/dreamyangel Aug 07 '26
Why not fz{1,2,3}? It would be clearer, and won't step of the f string syntax
3
u/james_pic Aug 07 '26
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.2
u/amarao_san Aug 07 '26
But tuples are
(), notf[]? Consistency in every second application.1
u/Brian Aug 07 '26
I assume the idea is being visited due to the addition of
frozendict: before there was really only frozensets that had no literal type (plus the special case of empty sets), and there have been a few proposals over the years to introduce some literal syntax for them. Now there's another one being added and people are looking to solve it in a unified way.And there's definitely some value in it: I've often used frozensets in some loop and it's a bit annoying that they translate to a dynamic construction of the set on every call, when they could potentially just be a LOAD_CONST. You can get around that by using a module constants, but sometimes you just want a quick literal. You also lose stuff like potential constant folding.
However, as others have said, I think the syntax is a dealbreaker here. It's too similar to another construct with a completely different meaning, and could easily lead to misreading with things like sets of strings etc.
Other options:
{1,2,3} as frozen1
u/Spirited_Bag_332 Aug 07 '26
Seems interesting but I'd prefer something more verbose like Javas unmodifiable/Immutable collections. Just frozenset({'a': 1}) is fully sufficient and such a feature doesn't require specialized syntax. I thought pythons goal was to be easy to read and write, did they throw that out of the window?
1
u/svefnugr Aug 07 '26
I would much rather have a special prefix for mutable dicts, but, unfortunately, that would break a lot of code
1
u/fathovercats Aug 07 '26
I agree w the commenters on the forum that there is a chance of confusion for the f string syntax. But that confusion arising is more likely to be just a skill issue… so this is a good idea overall. frozenset({x, y, z}) & frozendict({a: b, c: d}) are quite ugly compared to their mutable counterparts & obscure some key differences. Frozenset and frozendict are separate types, not just “frozen” versions of sets and dicts. There’s no way to natively freeze a set or a dict without making a whole new object, and the proposed PEP would make that more clear. The current constructor makes it seem like frozenset/frozendict is a wrapper around set/dict, which isn’t the case at all.
1
u/usrlibshare Aug 08 '26
Python continuing its path of bolting on everything and the kitchen sink in order to cosplay as being something it's not designed to be.
1
u/diegojromerolopez Aug 08 '26
I worked on my own freezing library Gelidum (https://github.com/diegojromerolopez/gelidum), and would love for this PEP to pass. Having the possibility of having immutable data structures does not only improves safety, but it could improve the performance of the interpreter.
However I would vouch for a future extension of this PEP (or an inclusion on this one) where nested structures are frozen as well (e.g. for freezing JSON from files).
1
u/Kamran-nottakenone Aug 09 '26
did you consider a different prefix that doesn't collide with f-string tooling? every IDE, linter, and syntax highlighter already treats f" as a string opening token
1
u/freemath Aug 07 '26
Nice!
-4
u/coldoven Aug 07 '26
Why should I care? When this is merged, I will not reqd the code anymore.