Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python单例,以2为例_Python_Singleton - Fatal编程技术网

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'>