Python单例模式的实现(基于__new__方法)

 1 class Solution:
 2     # @return: The same instance of this class every time
 3     import threading
 4     _instance_lock = threading.Lock()
 5     
 6     @classmethod
 7     def getInstance(cls):
 8         if not hasattr(cls, '_instance'):
 9             with cls._instance_lock:
10                 cls._instance = object.__new__(cls)
11         return cls._instance
12   

 

posted @ 2020-04-11 21:11  アカツキ  阅读(189)  评论(0编辑  收藏  举报