Assume I have 4 classes class ModelA, class ModelB, class ModelC class ModelD.
I have another class LongModel. I want to choose from the above 4 classes as the BaseClass for the class LongModel, based on model_name. What's the right way to do it. As of now, I am doing the following:
def get_long_model(model_name):
if model_name == 'A':
base_class = Model_A
if model_name == 'B':
base_class = Model_B
if model_name == 'C':
base_class = Model_C
if model_name == 'D':
base_class = Model_D
return LongModel(base_class)
I really don't want to use a function. Can we do this over the LongModel itself?
For eg:
class LongModel():
def __init__(self, model_name):
if model_name == 'A':
# Assign base class for LongModel
or some neat way using class or anything.
If I use composition then,
class LongModel():
def __init__(self, model):
self.model = model
long_model = LongModel(ModelA)
Then, assume ModelA has some attributes, which I want to use inside LongModel say the name, I have to add self.name = model.name, for one or 2 attributes its okay. But for all/most attributes isn't it difficult?
class LongModel(BaseClass):, so no, you can't do what you want. Use composition.LongModelclass object. You can’t dynamically switch out its base, since that changes it for every instance ofLongModel. That’s simply not how that relationship works.