I want to make a python singleton class and so that the instance of that class is exactly the same when everywhere else in the code calls the class after the program is on.
I tried to find singleton example and found one from link: https://wikidocs.net/3693
class Singleton(object):
_instance = None
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)
return class_._instance
and then testing like this
class Test(Singleton):
def __init__(self, name=None):
print(name)
test1 = Test('a')
expecting to show name that is given when it is initialized but this gives me an error
TypeError Traceback (most recent call last)
<ipython-input-55-f3383e846037> in <module>()
----> 1 test1 = Test('a')
<ipython-input-33-222ac7a13884> in __new__(class_, *args, **kwargs)
27 def __new__(class_, *args, **kwargs):
28 if not isinstance(class_._instance, class_):
---> 29 class_._instance = object.__new__(class_, *args, **kwargs)
30 return class_._instance
TypeError: object() takes no parameters
But when I try this, this one success
Test() # with no prarameter
test1 = Test('a') # this prints 'a' without error
I would like to know how to fix this. Otherwise, I have to initialize without parameters at the beginning when I start the program.
The reason I want to get the parameter is that when the parameter is changed at some point then I want to apply the changed information to everywhere that calls this class.