Hi all,
@sobolevn and I would like to propose PEP 841, which adds frozen display
syntax: f{1, 2, 3} creates a frozenset and f{'a': 1} creates a
frozendict.
Why we think Python needs this:
frozensetandfrozendictare builtin immutable types, but they
have no display syntax. Today the only way to create them is calling
the builtin functions.- Dedicated syntax makes optimization much easier. The compiler gets a
guarantee that the result is immutable, so it can fold constant
displays into constants. There are other approaches, but they are much harder than using a
guarantee that the syntax itself provides. f{...}looks similar to f-string syntax, but they are different at
the tokenizer level, so there is no conflict. Also, f-strings produce
an immutable string anyway, so thefprefix consistently gives you
an immutable value.- Based on the above, we believe this is a needed step toward a safer
future Python, where immutable data is easy to write and cheap to use.
PR for the PEP: PEP 841: Adding Frozen Syntax to Make Immutable Types Optimizable by corona10 · Pull Request #5049 · python/peps · GitHub
Reference implementation: GitHub - corona10/cpython at fset_fdict · GitHub
We will keep this discussion open for at least two weeks before moving
forward. Any feedback is welcome.
cc @vstinner