首页 > 编程语言 > 详细

python 设计模式

时间:2019-09-21 13:16:43      阅读:90      评论:0      收藏:0      [点我收藏+]

单例模式

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)

 

python 设计模式

原文:https://www.cnblogs.com/huay/p/11562199.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!