-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embrace the frozen set #3
base: main
Are you sure you want to change the base?
Conversation
@blhsing, I managed to implement frozen set literals. If the syntax needs to be modified that's very easy, all other code is already in place. I'll discuss this in 2025 on Discourse. |
Great work. Looks very good to me so far. Will try to poke holes in it later. |
I made it symmetrical: |
Should we use frozenset literals in the representation of subclasses? Using them is faster than regular sets. >>> class Set(set): pass
...
>>> Set({1, 2, 3})
Set({1, 2, 3}) # use Set({{1, 2, 3}}) instead It also simplifies the logic a bit. |
I've used the new syntax throughout the standard library (excluding tests). That made me find a library I forgot to update. |
The pull request is already way too large to review, so I'll have to split it up anyway. But this way we can show what the Python codebase could look like in a couple years and I'll have less work to figure things out if we decide to implement it anyway. We might be able to do this with a
It's not, but I didn't like the fact that this would raise a syntax error when evaluated and that Could you ask for some initial feedback on Discourse in the help category? (My account was suspended) |
Fair enough. Perhaps the old repr output can go through a deprecation process to give people time to update their codebase.
Ahh I've just found where this behavior is documented. It's called postponed evaluation of annotations as documented in PEP 563, where with Sorry to hear about your suspended account by the way. I was wondering why you have to wait until 2025 to present this idea but I suppose that's when your account can be active again. |
That makes total sense. Still slightly annoyed that frozenset literals have to cost 2 more characters but the benefit in simplified bytecode is clearly there. |
Great job finding usages in the stdlib, signifying the usefulness of the change. |
My account is suspended until September, but because I made too many half baked suggestions I was asked to wait until 2025. |
@Gouvernathor, would it make sense to already think about frozendict literals? Because changing the repr in the next version would be a breaking change. Most of what I did in this proof of concept could be adjusted for frozen dict literals. And the PEP could be called: "Embrace the frozen dict" (literally). It might also be better to add frozen dict literals first. Otherwise people will ask for |
I'm not sure how I feel about this proposition in itself. I'd be intuitively in favor of a |
Opinions vary, some find it intuitive, others find it very UNintuitive. From what I have gathered there are simply less issues with |
Frozen set literals and comprehensions
We should embrace the frozen set, literally:
{{...}}
.Motivation
Currently you need too many characters to construct a frozen set:
Additionally, this is very inefficient:
That's why it could be useful to have frozen set literals and comprehensions:
Then this is all we need to do:
Syntax
Note
Technically the syntax is this:
But that's an implementation detail that shouldn't be documented.
Examples
Example 1
Note
These are still type errors:
And these are now syntax errors:
Example 2
Note
This is still a type error:
And this is now a syntax error:
Backwards compatibility
These statements would behave differently with this proposal:
But I don't think they can be used for anything useful,
-''
is a shorter way to raise a type error.Pros
{{...}}
Cons
Double punctuation isn't Pythonic, there's a precedent with triple quotes:'''''+'''''
GitHub usage
frozenset({...})
Other suggestions for frozenset literals
expand
{1, 2, 3}.freeze()
Example:
Pros:
Cons:
{1, 2, 3}
Example:
Pros:
Cons:
|1, 2, 3|
Example:
Cons:
<1, 2, 3>
Example:
Pros:
Cons:
f{1, 2, 3}
Example:
Pros:
Cons:
foo{...}
s{}
for empty set{{1, 2, 3}}
Example:
Pros:
{{...}}
Cons:
Double punctuation isn't Pythonic, there's a precedent with triple quotes:'''''+'''''
|{1, 2, 3}|
Example:
Cons:
Links
📚 Documentation preview 📚: https://cpython-previews--3.org.readthedocs.build/