From 4edfa82607397d762df2bfcdaa5a109c9a70b0cc Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Sat, 7 Dec 2024 18:24:18 -0500 Subject: [PATCH] Improve Singleton metaclass --- dotbot/util/singleton.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/dotbot/util/singleton.py b/dotbot/util/singleton.py index 3776cb9..5891d8b 100644 --- a/dotbot/util/singleton.py +++ b/dotbot/util/singleton.py @@ -1,7 +1,9 @@ class Singleton(type): - _instances = {} - def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] + if not hasattr(cls, "_singleton_instance"): + cls._singleton_instance = super(Singleton, cls).__call__(*args, **kwargs) + return cls._singleton_instance + + def reset_instance(cls): + if hasattr(cls, "_singleton_instance"): + del cls._singleton_instance