from threading import Lock
class myClass(object):
myLock=Lock()
flag=None
def __init__(self,name):
self.n=name
print("aiay",name)
def __new__(cls, *args,**kwargs):
if cls.flag:
return cls.flag
else:
with cls.myLock:
if cls.flag ==None:
cls.flag=super().__new__(cls)
return cls.flag
if __name__=='__main__':
try:
sdf=myClass('abee')
print(sdf.n) #abee
sdf1=myClass('eee')
print(sdf is sdf1)#True
print(sdf.n)# eee
except:
traceback.print_exc()