Reputation: 929
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.
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)
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?
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.
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
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
import SingletonClass
class AnotherClass:
print SingletonClass.SingletonClass() #OUTPUT: 0x00000000030F1630
Lesson: Don't have your main function in the same module as your Singleton!
Upvotes: 4
Views: 2326
Reputation: 154514
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.
Upvotes: 2