python通過__new__實現單例模式


1 class Singleton(object):
2     def __new__(cls, *args, **kwargs):
3         if not hasattr(cls, '_instance'):
4             cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
5         return cls._instanc
class Singleton(object):
   _instance = None
def __new__(cls, *args, **kwargs): if not _instance: cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs) return cls._instanc

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com