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.

classification
Title: deepcopy(frozenset()) returns a new object
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Prometheus3375, rhettinger
Priority: normal Keywords:

Created on 2021-07-21 21:20 by Prometheus3375, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg397961 - (view) Author: Svyatoslav (Prometheus3375) * Date: 2021-07-21 21:20
from copy import copy, deepcopy
t = 1, 2
t1 = t, 3
t2 = t1, 4
t3 = t2, 5
assert copy(t3) is t3
assert deepcopy(t3) is t3

s = frozenset({1, 2})
assert s.copy() is s  # method .copy() always returns self, what its purpose?
assert copy(s) is s
assert deepcopy(s) is s  # raises AssertionError


Even a deepcopy of a tuple with many nested tuples is the the tuple itself, while a deepcopy of a frozenset which by definition cannot contain mutable objects is a new frozenset.
I think it is an error. A new frozenset is created because deepcopy() fallbacks to pickling.
msg397969 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-07-21 23:44
Returning the same object is just a micro-optimization and we've decided that it isn't worth it.  In bpo-40521, frozenset() stop being a singleton.  This costs a small bit of space but saves code complexity and a bit of execution time (especially given that we no longer have cheap access to global state).

Thanks for the suggestion, but we'll pass.
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88869
2021-07-21 23:44:16rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg397969

resolution: not a bug
stage: resolved
2021-07-21 21:32:29Prometheus3375settype: behavior
2021-07-21 21:20:08Prometheus3375create