In python, I want to design two classes :
- class A which will have a set of methods.
- class B which will have all the method of A and be a singleton.
I wrote it like this :
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class A:
def __init__(self, toto):
self.toto = toto
def another_method(self):
...
class B(A, metaclass=Singleton):
pass
is it the proper way to define A and B ?
Aucun commentaire:
Enregistrer un commentaire