一、classmethod装饰器
# 全局变量 ip = '192.168.13.98' port = '3306' class MySQL: __instance = None def __init__(self, ip, port): self.ip = ip self.port = port @classmethod def instance(cls, *args, **kwargs): if args or kwargs: cls.__instance = cls(*args, **kwargs) return cls.__instance obj1 = MySQL.instance(ip, port) obj2 = MySQL.instance() obj3 = MySQL.instance() print(obj1) print(obj2, obj2.__dict__) print(obj3, obj3.__dict__)
输出结果
<main.MySQL object at 0x058D6F30>
<main.MySQL object at 0x058D6F30> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL object at 0x058D6F30> {'ip': '192.168.13.98', 'port': '3306'}
二、类的装饰器
def singlegon(cls): _instance = cls(ip, port) def wrapper(*args, **kwargs): if args or kwargs: return cls(*args, **kwargs) return _instance return wrapper @singlegon class MySQL1: def __init__(self, ip, port): self.ip = ip self.port = port obj1 = MySQL1() obj2 = MySQL1() obj3 = MySQL1('1.1.1.3', 8080) print(obj1) print(obj2, obj2.__dict__) print(obj3, obj3.__dict__)
运行结果
<main.MySQL1 object at 0x04C102B0>
<main.MySQL1 object at 0x04C102B0> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL1 object at 0x04C10310> {'ip': '1.1.1.3', 'port': 8080}
三、元类
class Mymetaclass(type): def __init__(self, class_name, class_bases, class_dic): super().__init__(class_name, class_bases, class_dic) self.__instance = self(ip, port) def __call__(self, *args, **kwargs): if args or kwargs: obj = self.__new__(self) self.__init__(obj, *args, **kwargs) self.__instance = obj return self.__instance class MySQL2(metaclass=Mymetaclass): def __init__(self, ip, port): self.ip = ip self.port = port obj1 = MySQL2() obj2 = MySQL2() obj3 = MySQL2('1.1.1.3', 80) print(obj1) print(obj2, obj2.__dict__) print(obj3, obj3.__dict__)
运行结果
<main.MySQL2 object at 0x04D003B0>
<main.MySQL2 object at 0x04D003B0> {'ip': '192.168.13.98', 'port': '3306'}
<main.MySQL2 object at 0x04D003D0> {'ip': '1.1.1.3', 'port': 80}
四、模块导入
# instance.py class MySQL: def __init__(self, ip, port): self.ip = ip self.port = port ip = '192.168.13.98' port = 3306 instance = MySQL(ip, port) # 测试代码 import os, sys sys.path.append(os.path.dirname(os.path.dirname(__file__))) from test import instance obj1 = instance.instance obj2 = instance.instance obj3 = instance.MySQL('1.1.1.3', 80) print(obj1) print(obj2, obj2.__dict__) print(obj3, obj3.__dict__)
运行结果
<day30.instance.MySQL object at 0x052B0AB0>
<day30.instance.MySQL object at 0x052B0AB0> {'ip': '192.168.13.98', 'port': 3306}
<day30.instance.MySQL object at 0x052B03F0> {'ip': '1.1.1.3', 'port': 80}
五、重写__new__()
class MySQL3(object): __instance = None __first_init = True def __init__(self, ip, port): if self.__first_init: self.ip = ip self.port = port self.__first_init = False def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = object.__new__(cls) return cls.__instance obj1 = MySQL3(ip, port) obj2 = MySQL3(ip, port) obj3 = MySQL3('1.1.1.3', 80) print(obj1) print(obj2, obj2.__dict__) print(obj3, obj3.__dict__)
运行结果
<main.MySQL3 object at 0x059603F0>
<main.MySQL3 object at 0x059603F0> {'ip': '192.168.13.98', 'port': '3306', '_MySQL3__first_init': False}
<main.MySQL3 object at 0x059603F0> {'ip': '192.168.13.98', 'port': '3306', '_MySQL3__first_init': False}
注:前四种可以实现单例模式,但都不是绝对单例模式,可以创建新的对象,但是第五种方式是绝对单例模式,全局只能真正创建一次对象
以上就是python 实现单例模式的5种方法的详细内容,更多关于python 单例模式的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章