python 设计模式

单例模式

class 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)
原文地址:https://www.cnblogs.com/huay/p/11562199.html