您的位置:首页 > 编程语言 > Python开发

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: