-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.py
executable file
·29 lines (19 loc) · 913 Bytes
/
singleton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
MetaClass is a class that inherits type. Type is called when an object is created. class MyObject is equivalent to
type(MyObject, bases, args) something like that.
Here a metaclass returns the instance that has already been created. This method is preferred over the plain ol new
dunder method returning the instance. In that method, properties do not behave correctly.
"""
class SingletonMeta(type):
def __init__(cls, name, bases, attrs, **kwargs):
super().__init__(name, bases, attrs)
cls._instance = None
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__call__(*args, **kwargs)
return cls._instance
class SingletonSubject(metaclass=SingletonMeta):
def __init__(self):
# Put any initialization here.
self.__observers = set()
self.__subject_state = {}