-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.py
More file actions
34 lines (29 loc) · 908 Bytes
/
Singleton.py
File metadata and controls
34 lines (29 loc) · 908 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
# 元类实现Singleton
class Singleton(type):
def __init__(cls, name, base, dict):
super(Singleton,cls).__init__(name, base, dict)
cls._instance = None
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(Singleton,cls).__call__(*args, **kwargs)
return cls._instance
# new方法实现Singleton
class Singleton1(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls,'_instance'):
cls._instance = super(Singleton1,cls).__new__(cls)
return cls._instance
class myClass(metaclass=Singleton):
def __init__(self, hello= None):
self.hello = 'hello'
def foo(self):
pass
class myClass1(Singleton1):
def foo(self):
pass
def __init__(self, name):
self.name = name
demo1 = myClass1(1)
print(id(demo1))
demo2 = myClass1(2)
print(id(demo2))