Implemented Singleton metaclass

This commit is contained in:
tylerlaberge
2016-07-17 21:29:52 -04:00
parent 8e26a8b0d8
commit 13eac458e5
4 changed files with 91 additions and 32 deletions
+17 -2
View File
@@ -1,4 +1,19 @@
class Singleton(object):
pass
class Singleton(type):
"""
Singleton Metaclass.
Enforces any object using this metaclass to only create a single instance.
"""
__instance = None
def __call__(cls, *args, **kwargs):
"""
Override the __call__ method to make sure only one instance is created.
"""
if cls.__instance is None:
cls.__instance = type.__call__(cls, *args, **kwargs)
return cls.__instance