Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid storing empty singleton on class itself in python implementation #86

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/frozendict/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def immutable(self, *args, **kwargs):

raise AttributeError(f"'{self.__class__.__name__}' object is read-only")

_empty_frozendict_singleton = None
apmorton marked this conversation as resolved.
Show resolved Hide resolved

class frozendict(dict):
r"""
A simple immutable dictionary.
Expand Down Expand Up @@ -50,11 +52,12 @@ def __new__(e4b37cdf_d78a_4632_bade_6f0579d8efac, *args, **kwargs):
# empty singleton - start

if self.__class__ == frozendict and not len(self):
try:
self = cls.empty
global _empty_frozendict_singleton
apmorton marked this conversation as resolved.
Show resolved Hide resolved
if _empty_frozendict_singleton is None:
Marco-Sulla marked this conversation as resolved.
Show resolved Hide resolved
_empty_frozendict_singleton = self
else:
self = _empty_frozendict_singleton
continue_creation = False
except AttributeError:
cls.empty = self

# empty singleton - end

Expand Down