-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingletonPaternDesign.py
More file actions
37 lines (24 loc) · 875 Bytes
/
Copy pathsingletonPaternDesign.py
File metadata and controls
37 lines (24 loc) · 875 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
class Singleton:
instance = None
def __new__(cls):
if not cls.instance:
cls.instance = super().__new__(cls)
return cls.instance
class NotSingleton():
pass
#Example
#When using NotSingleton Class the output of the id instance id is the defferent
notSingleton1=NotSingleton()
print("notSingleton1 ----->"+str(id(notSingleton1)))
notSingleton2=NotSingleton()
print("notSingleton2 ----->"+str(id(notSingleton2)))
notSingleton3=NotSingleton()
print("notSingleton3 ----->"+str(id(notSingleton3)))
print("*************************************")
#When using Singleton Class the output of the instance id is the same
singleton1=Singleton()
print("singleton1 ----->"+str(id(singleton1)))
singleton2=Singleton()
print("singleton2 ----->"+str(id(singleton2)))
singleton3=Singleton()
print("singleton3 ----->"+str(id(singleton3)))