Python单例模式
来源:互联网 发布:张家港网络ankin 编辑:程序博客网 时间:2024/05/28 16:13
工厂方法
class Class3(object): def __init__(self): self.id = 0 @staticmethod def instance(*args, **kwargs): if not hasattr(Class3, "_instance"): Class3._instance = Class3(*args, **kwargs) return Class3._instancec1 = Class3.instance()c2 = Class3.instance()print(id(c1), c1.id)c1.id = 1000print(id(c2), c2.id)#===============(140619211572304, 0)(140619211572304, 1000)
metaclass 元类
class singleton(type): def __init__(cls, name, bases, dict): super(singleton, cls).__init__(name, bases, dict) def __call__(cls, *args, **kwargs): if not hasattr(cls, "_instance"): cls._instance = super(singleton, cls).__call__(*args, **kwargs) return cls._instanceclass Class4(object): __metaclass__ = singleton def __init__(self): self.id = 0c1 = Class4()c2 = Class4()print(id(c1), c1.id)c1.id = 1000print(id(c2), c2.id)#=============(139687817574160, 0)(139687817574160, 1000)
0 0
- Python单例模式
- Python单例模式
- 单例模式(python)
- python单例模式
- python单例模式
- python 单例模式
- python单例模式
- python单例模式
- python单例模式
- Python 单例模式
- Python 单例模式
- Python -- 单例模式
- Python单例模式
- Python单例模式
- 单例模式--Python
- python 单例模式
- python单例模式
- python-单例模式
- 图像特效---怀旧风格滤镜
- uva 1585 - Score
- 剑指offer之面试题7用两个栈实现一个队列
- 摩托车继承自行车和机动车
- Effective C++----3rd Edition, Item 3:尽可能使用const
- Python单例模式
- 第九周第二项目——深复制与浅复制
- C语言基础——getchar()函数的作用
- 我的CSDN情结
- C/C++程序运行时进程的内存分布情况
- Fedora 16 系统安装
- 图像特效---暗调滤镜
- Android虚拟机怎么启动不起来,一直停在启动界面
- 重新学javaweb---文件下载