总是想不到什么好办法在python里写一个完美的Singleton,参考了好多实例,想出了这么个办法[原创]关于python的Singleton

 

class Singleton(object):
    
__instance = None

    
def __new__(classtype, *args, **kwargs):
        
if classtype != type(classtype.__instance):
            classtype.
__instance = object.__new__(classtype, *args, **kwargs)
            classtype.
__instance.init()

        
return classtype.__instance


    
def init(self):
        
pass

 

 

 这样既解决了子类化问题,又解决了__init__重复执行的问题。

 

相关文章:

  • 2022-12-23
  • 2021-08-03
  • 2021-07-27
  • 2021-12-23
  • 2022-02-11
  • 2021-05-18
  • 2021-12-27
  • 2021-10-29
猜你喜欢
  • 2021-11-10
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2021-09-13
  • 2022-01-09
相关资源
相似解决方案