[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-17 Thread Mark Dickinson
Change by Mark Dickinson : -- nosy: -mark.dickinson ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: https://

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-17 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: As Steven have noted the compiler-time optimization is not applicable here because name frozenset is resolved at run-time. In these cases where a set of constants can be replaced with a frozenset of constants (in "x in {1,2,3}" and in "for x in {1,2,3}") t

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-16 Thread Terry J. Reedy
Terry J. Reedy added the comment: Rejected by the reality of Python's dynamism, which I overall appreciate ;-). -- ___ Python tracker ___ _

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-16 Thread Mark Dickinson
Mark Dickinson added the comment: > That's not always the case though. Sorry, yes - I see. We're not creating a frozenset from a frozenset - we're creating a frozenset from a regular set from a frozenset. :-( Sorry for the noise. -- ___ Python tr

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-16 Thread Steven D'Aprano
Steven D'Aprano added the comment: That's not always the case though. >>> def f(): ... return frozenset({1, 2, 3}) ... >>> a = f.__code__.co_consts[1] >>> a frozenset({1, 2, 3}) >>> b = f() >>> assert a == b >>> a is b False Also see the disassembly I posted on Python-Ideas. --

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-16 Thread Mark Dickinson
Mark Dickinson added the comment: [Terry] > To avoid the intermediate set, [...] It's not quite as bad as that: there _is_ no intermediate set (or if you prefer, the intermediate set is the same object as the final set), since the frozenset call returns its argument unchanged if it's already

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-16 Thread Steven D'Aprano
Steven D'Aprano added the comment: Proposed on Python-Ideas. https://mail.python.org/archives/list/python-id...@python.org/message/GRMNMWUQXG67PXXNZ4W7W27AQTCB6UQQ/ -- ___ Python tracker ___

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-15 Thread Dennis Sweeney
Dennis Sweeney added the comment: There's also the hacky expression {*()} to get an empty set -- nosy: +Dennis Sweeney ___ Python tracker ___ _

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-15 Thread Terry J. Reedy
Terry J. Reedy added the comment: Sigh. You are right. I will close this tomorrow. This also means that 'set()' is not guaranteed to return an empty built-in set. I did think of this workaround for that: >>> (empty:={None}).clear() >>> empty set() Go ahead and propose something on python

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-15 Thread Steven D'Aprano
Steven D'Aprano added the comment: The difficulty is that frozenset may have been shadowed, or builtins monkey-patched, so we cannot know what frozenset({1, 2, 3}) will return until runtime. Should we re-consider a frozenset display literal? f{1, 2, 3} works for me. -- nosy: +

[issue46393] Generate frozenset constants when explicitly appropriate

2022-01-15 Thread Terry J. Reedy
New submission from Terry J. Reedy : 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_SET0