参考博文:https://blog.csdn.net/sinat_38682860/article/details/103979764
------------------------------普通方式启动试试-----------------------------
第一种方法:重写__new__方法【默认单例模式:导入模块方式】
class Singleton(object):
_instance = None
def __init__(self,password):
self.password=password
def run(self,msg):
self.msg = msg
def __new__(cls,*args,**kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
if __name__ == '__main__':
info = "类对象ID:{} run方法对象ID:{} password(ID:{} 值:{}) msg(参数对象ID:{} 值:{})"
for i in range(0,5):
instance1 = Singleton(i)
print(info.format(id(instance1),id(instance1.run('你们好')),id(instance1.password),instance1.password,id(instance1.msg),instance1.msg))
for i in range(5,10):
instance2 = Singleton(i)
print(info.format(id(instance2),id(instance2.run('大家好')),id(instance2.password),instance2.password,id(instance2.msg),instance2.msg))
"""
输出:仔细查看对象ID和值
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129152 值:0) msg(参数对象ID:2787165497168 值:你们好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129184 值:1) msg(参数对象ID:2787165497168 值:你们好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129216 值:2) msg(参数对象ID:2787165497168 值:你们好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129248 值:3) msg(参数对象ID:2787165497168 值:你们好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129280 值:4) msg(参数对象ID:2787165497168 值:你们好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129312 值:5) msg(参数对象ID:2787164929264 值:大家好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129344 值:6) msg(参数对象ID:2787164929264 值:大家好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129376 值:7) msg(参数对象ID:2787164929264 值:大家好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129408 值:8) msg(参数对象ID:2787164929264 值:大家好)
类对象ID:2787163436368 run方法对象ID:140703145896064 password(ID:140703146129440 值:9) msg(参数对象ID:2787164929264 值:大家好)
"""
第二种方法:类方法实现
class Singleton:
_instance = None
def __init__(self, password):
self.password = password
def run(self, msg):
self.msg = msg
@classmethod # 方式一:使用__new__内置方法
def instance(cls, *args, **kwargs):
if not cls._instance:
cls._instance = object.__new__(cls)
return cls._instance
# @classmethod # 方式二.使用本身Singleton对象
# def instance(cls, *args, **kwargs):
# if not hasattr(Singleton, "_instance"):
# Singleton._instance = Singleton(*args, **kwargs)
# return Singleton._instance
if __name__ == '__main__':
info = "类对象ID:{} run方法对象ID:{} password(ID:{} 值:{}) msg(参数对象ID:{} 值:{})"
for i in range(0, 5):
instance1 = Singleton(i)
print(info.format(id(instance1), id(instance1.run('你们好')), id(instance1.password), instance1.password,id(instance1.msg), instance1.msg))
for i in range(5, 10):
instance2 = Singleton(i)
print(info.format(id(instance2), id(instance2.run('大家好')), id(instance2.password), instance2.password,id(instance2.msg), instance2.msg))
""" 出现问题【但是用多线程就可以,奇怪】
输出:仔细查看对象ID和值
类对象ID:1963939394896 run方法对象ID:140737431578752 password(ID:140737431811840 值:0) msg(参数对象ID:1963968130960 值:你们好)
类对象ID:1963968078032 run方法对象ID:140737431578752 password(ID:140737431811872 值:1) msg(参数对象ID:1963968130960 值:你们好)
类对象ID:1963939394896 run方法对象ID:140737431578752 password(ID:140737431811904 值:2) msg(参数对象ID:1963968130960 值:你们好)
类对象ID:1963968078032 run方法对象ID:140737431578752 password(ID:140737431811936 值:3) msg(参数对象ID:1963968130960 值:你们好)
类对象ID:1963939394896 run方法对象ID:140737431578752 password(ID:140737431811968 值:4) msg(参数对象ID:1963968130960 值:你们好)
类对象ID:1963968078032 run方法对象ID:140737431578752 password(ID:140737431812000 值:5) msg(参数对象ID:1963967560944 值:大家好)
类对象ID:1963968383824 run方法对象ID:140737431578752 password(ID:140737431812032 值:6) msg(参数对象ID:1963967560944 值:大家好)
类对象ID:1963968078032 run方法对象ID:140737431578752 password(ID:140737431812064 值:7) msg(参数对象ID:1963967560944 值:大家好)
类对象ID:1963968383824 run方法对象ID:140737431578752 password(ID:140737431812096 值:8) msg(参数对象ID:1963967560944 值:大家好)
类对象ID:1963968078032 run方法对象ID:140737431578752 password(ID:140737431812128 值:9) msg(参数对象ID:1963967560944 值:大家好)
"""
第三种方法:闭包定义装饰器,将类的定义隐藏到闭包函数中
def singleton(cls):
_instance = {
}
def _singleton(*args,**kwargs):
if cls not in _instance:
_instance[cls]