On Wed, Jan 19, 2022 at 6:31 PM Ben Rudiak-Gould <benrud...@gmail.com> wrote:
>
> My preferred syntax for a frozenset literal would be something like
>
>     {1, 2, 3}.freeze()
>
> This requires no new syntax, and can be safely optimized at compile time (as 
> far as I can tell).
>
> set.freeze would be a new method of sets which could also be used at run 
> time. It would return a new frozenset object and wouldn't alter the set 
> object (so perhaps the name I suggested isn't ideal). Of course 
> frozenset.freeze would just return itself.
>

+0.5. I'm not sure if CPython is currently optimizing this (I tried
"spam".upper() and it didn't constant-fold), but it certainly could.
Making this work would depend on several optimizations:

1) Recognize literal.method() as being potentially constant-foldable
2) Marke some methods as pure and therefore optimizable
3) Recognize that the (mutable) set to the left of .freeze() can be
frozen just as "a in {1,2,3}" can

But yes, in theory, this could work. There's no way that it can be
semantically wrong, no way to shadow that method.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YDESIUXVULQMTYTRT65FT7SSWER3EELA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to