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

python中的单例

时间:2018-11-02 15:58:06      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:nbsp   one   pytho   single   main   使用   def   print   str   

使用__new__

 1 class Single:
 2     instance = None
 3 
 4     def __new__(cls, *args, **kwargs):
 5         if cls.instance:
 6             return cls.instance
 7         cls.instance = object.__new__(cls)
 8         return cls.instance
 9 
10 o1 = Single()
11 o2 = Single()
12 print(o1)  # <__main__.Single object at 0x00000000021EDAC8>
13 print(o2)  # <__main__.Single object at 0x00000000021EDAC8>

 

python中的单例

标签:nbsp   one   pytho   single   main   使用   def   print   str   

原文地址:https://www.cnblogs.com/zze46/p/9896600.html

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