-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathsingleton_concept.py
More file actions
37 lines (26 loc) · 829 Bytes
/
singleton_concept.py
File metadata and controls
37 lines (26 loc) · 829 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
# pylint: disable=too-few-public-methods
"Singleton Concept Sample Code"
import copy
class Singleton():
"The Singleton Class"
value = []
def __new__(cls):
return cls
# def __init__(self):
# print("in init")
@staticmethod
def static_method():
"Use @staticmethod if no inner variables required"
@classmethod
def class_method(cls):
"Use @classmethod to access class level variables"
print(cls.value)
# The Client
# All uses of singleton point to the same memory address (id)
print(f"id(Singleton)\t= {id(Singleton)}")
OBJECT1 = Singleton()
print(f"id(OBJECT1)\t= {id(OBJECT1)}")
OBJECT2 = copy.deepcopy(OBJECT1)
print(f"id(OBJECT2)\t= {id(OBJECT2)}")
OBJECT3 = Singleton()
print(f"id(OBJECT1)\t= {id(OBJECT3)}")