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)
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.
from: https://stackoverflow.com/questions/63373883/python-make-a-derived-class-into-a-singleton
No comments:
Post a Comment