码迷,mamicode.com
首页 > 编程语言 > 详细

python 的单例

时间:2019-07-15 17:29:01      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:bsp   new   print   object   nis   *args   none   single   finish   

 

例子

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kw):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)  
        return cls._instance  

class MyClass(Singleton):  
    a = 1
one = MyClass()
two = MyClass()
print(one == two)
print(one is two)
print(id(one), id(two))

输出

True
True                                                        
499807697384 499807697384                                   
[Program finished]

 

python 的单例

标签:bsp   new   print   object   nis   *args   none   single   finish   

原文地址:https://www.cnblogs.com/sea-stream/p/11190046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!