I've tried recently to add a singleton to my project, but it doesn't act like I'd think it would. The code looks somewhat like this:
main/main.py
class main(metaclass=Singleton):
def __init__(self):
....
def Action(self):
self.helper=otherclasses.other.other()
if __name__ == "__main__":
m = main()
m.Action()
main/metaclasses/singleton.py
class Singleton(type):
_instance = None
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
def __call__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instance
def Instance(cls, *args, **kwargs):
return cls.__call__(*args, **kwargs)
main/otherclasses/other.py
class other():
def __init__(self):
...
self.main = Main.Instance()
...
so, as far as I understand this concept - I should have got the same instance of Main for subclass. Yet I get a whole new Main object. I'd be glad for some help! What am I missing?
Thanks in advance.