Improve Singleton metaclass
This commit is contained in:
parent
8d94c6ec1a
commit
4edfa82607
1 changed files with 7 additions and 5 deletions
|
@ -1,7 +1,9 @@
|
||||||
class Singleton(type):
|
class Singleton(type):
|
||||||
_instances = {}
|
|
||||||
|
|
||||||
def __call__(cls, *args, **kwargs):
|
def __call__(cls, *args, **kwargs):
|
||||||
if cls not in cls._instances:
|
if not hasattr(cls, "_singleton_instance"):
|
||||||
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
cls._singleton_instance = super(Singleton, cls).__call__(*args, **kwargs)
|
||||||
return cls._instances[cls]
|
return cls._singleton_instance
|
||||||
|
|
||||||
|
def reset_instance(cls):
|
||||||
|
if hasattr(cls, "_singleton_instance"):
|
||||||
|
del cls._singleton_instance
|
||||||
|
|
Loading…
Reference in a new issue