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 all commits
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
12 changes: 8 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 = None

class frozendict(dict):
r"""
A simple immutable dictionary.
Expand Down Expand Up @@ -50,11 +52,13 @@ 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

if _empty_frozendict is None:
_empty_frozendict = self
else:
self = _empty_frozendict
continue_creation = False
except AttributeError:
cls.empty = self

# empty singleton - end

Expand Down
Loading