-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.py
36 lines (28 loc) · 1.01 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
class Singleton:
"""A singleton class. Lazy instantiation, it is only created when needed"""
__instance = None
def __init__(self):
if Singleton.__instance is not None:
raise Exception(
"Singleton already created, use get_instance() instead")
else:
Singleton.__instance = self
@staticmethod
def get_instance():
""" this static method is associated with the class"""
if Singleton.__instance is None:
Singleton()
return Singleton.__instance
def main():
singleton1 = Singleton.get_instance()
singleton2 = Singleton.get_instance()
singleton3 = Singleton.get_instance()
print(singleton1)
print(singleton2)
print(singleton3)
if singleton1 == singleton2 == singleton3:
print("same memory location, pattern implemented correctly")
singleton1.counter = 27 # one instance is updated
print(singleton2.counter) # change reflected in others
if __name__ == '__main__':
main()