-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.py
More file actions
39 lines (25 loc) · 966 Bytes
/
singleton.py
File metadata and controls
39 lines (25 loc) · 966 Bytes
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
30
31
32
33
34
35
36
37
38
39
class Singleton(type):
def __new__(cls, name, bases, namespace):
namespace['_instance'] = None
return super(Singleton, cls).__new__(cls, name, bases, namespace)
class SingletonBase(metaclass=Singleton):
def __new__(cls, *args, **kwargs):
if cls._instance is None:
print("Create new instance for class {}".format(cls.__name__))
instanceof = super(SingletonBase, cls).__new__(cls)
cls._instance = instanceof
return instanceof
else:
print("The class {} has been instantiated".format(cls.__name__))
return cls._instance
class MyClass(SingletonBase):
def __init__(self,val):
self._val = val
if __name__ == '__main__':
x = MyClass(10)
y = MyClass(20)
print("pointer of x: {}".format(x))
print("pointer of y: {}".format(y))
assert x is y
print("x._val: {}".format(x._val))
print("y._val: {}".format(y._val))