[Python-ideas] anyone need a frozenset or bytearray literal?

Steven D'Aprano steve at pearwood.info
Thu Jul 12 05:51:46 EDT 2018

On Wed, Jul 11, 2018 at 04:25:56PM -0700, Gregory P. Smith wrote:
> Completely obvious what it does, but it irritates my aesthetic
> sensibilities every time I see:
>   frozenset({spam, eggs})

+1 to the idea of a new set method, freeze, returning a frozenset, and 
allowing the interpreter to optimize calls like:

   {a, b, c}.freeze() 

to skip making the temporary set.

I seem to have a vague recollection that there was already a CPython 
optimization in place that substitutes a frozenset for certain set 
displays... ah yes, here it is:

py> dis.dis("x in {2, 3}")
  1           0 LOAD_NAME                0 (x)
              3 LOAD_CONST               2 (frozenset({2, 3}))
              6 COMPARE_OP               6 (in)
              9 RETURN_VALUE



More information about the Python-ideas mailing list