python 设计模式
2020-12-13 14:27
标签:let == class rgs div instance python not return 单例模式 python 设计模式 标签:let == class rgs div instance python not return 原文地址:https://www.cnblogs.com/huay/p/11562199.htmlclass Singleton:
def __new__(cls, *args, **kw):
if not hasattr(cls, ‘_instance‘):
cls._instance = object.__new__(cls)
return cls._instance
def tt(self):
pass
one = Singleton()
two = Singleton()
two.a = 3
# 3
# one和two完全相同,可以用id(), ==, is检测
print(id(one))
# 29097904
print(id(two))
# 29097904
print(one == two)
# True
print(one is two)
上一篇:python函数作用域