跨模块单例 [英] Singleton across modules
问题描述
我正在尝试实现Singleton,并且在导入模块时遇到了困难。我的设置如下。我正在使用Python 2.7。
模块1
class SingletonClass( object):
def __new __(self,* args,** kwargs):
如果不是self._instance:
self._instance = super(SingletonClass,self).__ new __(
self,* args,** kwargs)
return self._instance
print SingletonClass()#输出:0x00000000030F1630
print SingletonClass()#输出:0x00000000030F1630(好,什么我想要)
模块2
import SingletonClass
class AnotherClass:
打印SingletonClass.SingletonClass()#输出:0x0000000003292208
在该模块中,单例正在工作,但在另一个模块中,单例未返回与第一个相同的对象。知道为什么吗?
编辑
现在,我将介绍我发现唯一可行的方法。我敢肯定有一个更好的解决方案,但是我认为这可以更好地传达潜在的问题。
MODULE 1
类SingletonParent(对象):
_instance = None
def __new __(self,* args,** kwargs):
如果不是self._instance:
self._instance = super(SingletonParent,self).__ new __(
self,* args,** kwargs)
return self._instance
模块2
import SingletonParent
class SingletonClass(object):
def __new __(self,* args,** kwargs):
如果不是SingletonParent.SingletonParent._instance:
SingletonParent。 SingletonParent._instance = super(SingletonClass,self).__ new __(
self,* args,** kwargs)
return SingletonParent.SingletonParent._instance
print SingletonClass()#OUTPUT : 0x00000000030F1630
打印SingletonClass()#输出:0x00000000030F1630
模块3
import SingletonClass
class AnotherClass:
打印SingletonClass.SingletonClass()#输出:0x00000000030F1630
解决方案(第3版)
课程:没有您的主要功能与Singleton在同一模块中!
您的问题很可能是该模块在两次导入下两次导入不同的名字。
要对此进行测试,请添加以下内容:
print正在导入...
在 module1.py
。
如果此消息被打印两次,则模块被导入两次,这就是您的问题。要解决此问题,请确保使用相同的名称将模块导入所有地方[0],并且不要使用 sys.path
进行黑客攻击。 / p>
[0]:从技术上讲这不是必需的,但这是一个简单的解决方法。
I'm trying to implement a Singleton and I am running into difficulty when I import the module. My set up is the following. I am using Python 2.7.
MODULE 1
class SingletonClass(object):
def __new__(self, *args, **kwargs):
if not self._instance:
self._instance = super(SingletonClass, self).__new__(
self, *args, **kwargs)
return self._instance
print SingletonClass() #OUTPUT: 0x00000000030F1630
print SingletonClass() #OUTPUT: 0x00000000030F1630 (Good, what I want)
MODULE 2
import SingletonClass
class AnotherClass:
print SingletonClass.SingletonClass() #OUTPUT: 0x0000000003292208
Within the module the singleton is working, but in another module the Singleton is not returning the same object as it did in the first. Any idea why?
Edit
For now I will put the only thing that I have found that works. I'm sure there is a better solution to this, but I think this may better convey what the underlying problem is.
MODULE 1
class SingletonParent(object):
_instance = None
def __new__(self, *args, **kwargs):
if not self._instance:
self._instance = super(SingletonParent, self).__new__(
self, *args, **kwargs)
return self._instance
MODULE 2
import SingletonParent
class SingletonClass(object):
def __new__(self, *args, **kwargs):
if not SingletonParent.SingletonParent._instance:
SingletonParent.SingletonParent._instance = super(SingletonClass, self).__new__(
self, *args, **kwargs)
return SingletonParent.SingletonParent._instance
print SingletonClass() #OUTPUT: 0x00000000030F1630
print SingletonClass() #OUTPUT: 0x00000000030F1630
MODULE 3
import SingletonClass
class AnotherClass:
print SingletonClass.SingletonClass() #OUTPUT: 0x00000000030F1630
Solution (Edit 3)
Lesson: Don't have your main function in the same module as your Singleton!
Your problem is most likely that the module is being imported twice under two different names.
To test for this, add something like:
print "Being imported..."
In module1.py
.
If this message is printed twice, then the module is being imported twice, and that's your problem. To fix it, make sure that you're using the same name to import the module everywhere[0], and that you're not doing hackery with sys.path
.
[0]: Technically this shouldn't be necessary, but it's a simple fix.
这篇关于跨模块单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!