Reputation: 271
I want to turn my derived class into a singleton in python. I would like to implement the singleton via metaclass, but I always come across the following error:
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
My Code:
# singleton.py
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]
# base.py
from abc import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def run(self, value):
pass
# foo.py
from base import Base
from singleton import Singleton
class Foo(Base, metaclass=Singleton):
def run(self, value):
print(value)
# main.py
from foo import Foo
f1 = Foo()
print(f1)
f1.run(42)
f2 = Foo()
print(f2)
f2.run(24)
Upvotes: 0
Views: 700
Reputation: 271
Through various other stackoverflow questions I have now come up with a solution myself.
As the error text already says, the metaclass of Foo
must be a metaclass that is compatible with the base class metaclass (=ABCMeta
).
This means that Singleton
must also inherit from ABCMeta
.
New Code:
# singleton.py
from abc import ABCMeta
class Singleton(ABCMeta):
# ...
# bar.py
from base import Base
class Bar(Base):
def run(self, value):
print(value)
# main.py
from foo import Foo
from bar import Bar
f1 = Foo()
print(f1)
f1.run(42)
f2 = Foo()
print(f2)
f2.run(34)
b1 = Bar()
print(b1)
b1.run(12)
b2 = Bar()
print(b2)
b2.run(21)
Output:
<foo.Foo object at 0x000001D4D512BE48>
42
<foo.Foo object at 0x000001D4D512BE48>
34
<bar.Bar object at 0x000001D4D512B0B8>
12
<bar.Bar object at 0x000001D4D512BEF0>
21
So Foo
is a singleton and Bar
isn't.
Bonus question:
Why doesn't Singleton(ABC)
work?
Upvotes: 3