首先我们的明白,当我们实例化一个对象时,是先执行了类的__new__方法(我们没写时默认调用object.__new__),实例化对象;
然后再执行类的__init__方法,对这个对象进行初始化
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self):
pass
def __new__(cls,*args,**kwargs):
if not hasattr(Singleton,"_instance"):
with Singleton._instance_lock:
Singleton._instance = object.__new__(cls)
return Singleton._instance
obj1 = Singleton()
obj2 = Singleton()
print(obj1,obj2)
def task(arg):
obj = Singleton()
print(obj)
for i in range(10):
t = threading.Thread(target=task,args=[i,])
t.start()
<__main__.Singleton object at 0x000002A6870055F8>
<__main__.Singleton object at 0x000002A6870055F8>
<__main__.Singleton object at 0x000002A6870055F8>
<__main__.Singl