单例方式
单例方式 通过类的绑定方法 class Person(): _instance=None def __init__(self,port,host): self.port=port self.host=host @classmethod def get_sigoleton(cls): import settings if not cls._instance: cls._instance = cls(settings.PORT, settings.HOST) return cls._instance s1=Person.get_sigoleton() s2=Person.get_sigoleton() s3=Person.get_sigoleton() print(s1) print(s2) s4=Person('33306','192.168.1.1') print(s4) 通过装饰器 def get_sigoleton(cls): import settings instance=cls(settings.PORT,settings.HOST) def wrapper(*args,**kwargs): if len(args)!=0 or len(kwargs)!=0 res=cls(*args,**kwargs) return res else: return instance