This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients serhiy.storchaka, terry.reedy
Date 2022-01-16.03:11:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The CPython compiler is capable of making frozenset constants without being explicitly asked to.  Exactly how it does so is, of course, 'hidden' from python code.  With current main:
>>> dis('{1,2,3}')
  1           0 BUILD_SET                0
              2 LOAD_CONST               0 (frozenset({1, 2, 3}))
              4 SET_UPDATE               1
              6 RETURN_VALUE

Suppose one wants actually wants a frozenset, not a mutable set.  'frozenset({1,2,3})' is compiled as the above followed by a frozenset call -- making an unneeded double conversion to get what already exists. 
To avoid the intermediate set, one can use a constant tuple instead.

>>> dis('frozenset((1,2,3))')
  1           0 LOAD_NAME                0 (frozenset)
              2 LOAD_CONST               0 ((1, 2, 3))
              4 CALL_FUNCTION            1
              6 RETURN_VALUE

Even nicer would be

  1           0 (frozenset({1, 2, 3}))
              2 RETURN_VALUE

'set((1,2,3))' is compiled the same as 'frozenset((1,2,3)), but frozenset does not having the option is using a more efficient display form.  I cannot think of any reason to not call frozenset during compile time when the iterable is a constant tuple.

Serhiy, I not sure how this relates to your issue 33318 and the discussion therein about stages, but it does relate to your interest in compile time constants.
Date User Action Args
2022-01-16 03:11:25terry.reedysetrecipients: + terry.reedy, serhiy.storchaka
2022-01-16 03:11:25terry.reedysetmessageid: <>
2022-01-16 03:11:25terry.reedylinkissue46393 messages
2022-01-16 03:11:25terry.reedycreate