-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathsingleton.py
72 lines (51 loc) · 1.53 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Code Listing #4
"""
All code listings for Singleton pattern shown in the book
"""
class MetaSingleton(type):
""" A type for Singleton classes (overrides __call__) """
def __init__(cls, *args):
print(cls,"__init__ method called with args", args)
type.__init__(cls, *args)
cls.instance = None
def __call__(cls, *args, **kwargs):
if not cls.instance:
print(cls,"creating instance", args, kwargs)
cls.instance = type.__call__(cls, *args, **kwargs)
return cls.instance
class Singleton(object):
""" Singleton in Python """
_instance = None
def __new__(cls):
if cls._instance == None:
cls._instance = object.__new__(cls)
return cls._instance
class SingletonA(Singleton):
pass
class SingletonA1(SingletonA):
pass
class SingletonB(Singleton):
pass
class SingletonM(metaclass=MetaSingleton):
pass
def test_single(cls):
""" Test if passed class is a singleton """
return cls() == cls()
if __name__ == "__main__":
# Check for state sharing across hierarchies
a = SingletonA()
a1 = SingletonA1()
b = SingletonB()
a.x = 100
print('a.x =>',a.x)
print('a1.x =>',a1.x)
# Will raise an exception
try:
print('b.x =>',b.x)
except AttributeError as e:
print('Error:',e)
print(test_single(Singleton))
print(test_single(SingletonM))
print(test_single(SingletonA))
print(test_single(SingletonB))
print(test_single(SingletonA1))