I have a few dozen classes. Here are two of them:
class Class_A(ClassABC):
def __init__(self):
super().__init__()
def from_B(self, b):
#do stuff
def from_C(self, c):
#do stuff
#...
def to_B(self):
rt = Class_B()
rt.from_A(self)
return rt
def to_C(self):
rt = Class_C()
rt.from_A(self)
return rt
#...
class Class_B(ClassABC):
def __init__(self):
super().__init__()
def from_A(self, a):
#do stuff
def from_C(self, c):
#do stuff
def to_A(self):
rt = Class_A()
rt.from_B(self)
return rt
def to_C(self):
rt = Class_C()
rt.from_B(self)
return rt
#...
#class Class_C, Class_D, Class_E, etc,
and here is the ABC:
class ClassABC(metaclass=abc.ABCMeta):
@abc.abstractmethod
def __init__(self):
#do stuff
The problem I have is that all the to_*
methods in the subclasses follow the same exact pattern, and it becomes tedious to implement them. I would like to automatically generate them in the ClassABC
if possible, but so far I have failed. I also tried creating a class decorater for the subclasses, but that didn't work either. I have, however, managed to auto generate the methods in each subclass using exec(), but I rather have the ABC generate them or use class decoraters. Is there a way to do this?
Note: all the classes are in their own separate module