Python 3.6 单例模式 __new__实现
2017-11-14 14:35
357 查看
# ----------------------- # __Author : tyran # __Date : 17-11-14 # ----------------------- class Base: __instance = None def __init__(self, num): self.num = num def show(self): print(self.num) def __new__(cls, *args, **kwargs): if cls.__instance is None: cls.__instance = super().__new__(cls) return cls.__instance b1 = Base(2) b1.show() b2 = Base(3) b1.show() b2.show()
相关文章推荐
- 使用 __new__ 实现 Python 的单例模式
- python实现单例模式__init__ __new__区别
- python中的__new__方法与__init__方法区别与调用;通过使用__new__实现单例模式
- python中7种实现单例模式的方法:staticmethod、classmethod、类属性方法、__new__、装饰器、元类、名字覆盖
- python实现单粒模式,模块方法/装饰器方法/__new__方法
- Python单例模式的4种实现方法
- python修饰器实现单例模式
- python优雅实现策略模式
- Python单例模式的4种实现方法
- Python 实现单例模式
- Python 实现单例模式
- 研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)
- 在 Python 中实现单例模式
- Python单例模式的4种实现方法
- python中如何简单的实现decorator模式--由django admin源码所得体会
- 23种设计模式之python实现--解释器模式
- python 元类型编程, 单例模式SingleTon的一种实现方式
- [python实现设计模式]-1. 单例模式
- Python单例模式的4种实现方法
- python 设计模式实现