Python单例,以2为例
请先阅读以下内容: 结果:Python单例,以2为例,python,singleton,Python,Singleton,请先阅读以下内容: 结果: Singleton.__new__ called with class <class '__main__.Cache'> Singleton.__new__ creating instance of class <class '__main__.Cache'> Cache.__init__ called with size 0 Singleton.__new__ called with class <class '__main__.Cac
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ creating instance of class <class '__main__.Cache'>
Cache.__init__ called with size 0
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.\uuuuu new\uuuuuu用类调用
Singleton.\uuuu新建\uuuu创建类的实例
缓存.\uuuu init\uuuuuu被调用,大小为0
Singleton.\uuuuu new\uuuuuuuu与类一起调用
Singleton.\uuuuu new\uuuuuuuu与类一起调用
Singleton.\uuuuu new\uuuuuuuu与类一起调用
Singleton.\uuuuu new\uuuuuuuu与类一起调用
它现在似乎可以工作了,但问题是在Singleton中显式调用inhering classinit是否是pythonic的?有没有可能出问题的smth?不起作用。实际上,您从未从
\uuuu new\uuuu
返回任何内容,因此它将返回默认值None
<代码>cls.\u实例设置正确,但调用缓存时无法获得cls.\u实例
这是针对个人的:
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ creating instance of class <class '__main__.Cache'>
Cache.__init__ called with size 0
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>
Singleton.__new__ called with class <class '__main__.Cache'>