houssamzenati
houssamzenati

Reputation: 35

Can a class inherit from an "argument" class? (Conditional inheriting)

I am using python 3.6 and would like to build a subclass from different possible classes.

So a toy example would be:

class MNISTArchitecture:
    def __init__(self, inputs):
        self.input_pl = inputs
        self.learning_rate = LEARNING_RATE
        self.batch_size = BATCH_SIZE
        self.encoder
        self.decoder 

class CIFARArchitecture:
    def __init__(self, inputs):
        self.input_pl = inputs
        self.learning_rate = LEARNING_RATE
        self.batch_size = BATCH_SIZE
        self.encoder
        self.decoder

And then build a class that could inherit from first architecture or the second one:

class AutoEncoder(Architecture):
    def __init__(self, input_pl, dataset):
        super().__init__(input_pl)
        self.dataset = dataset
        self.anomalous_label = anomalous_label
        self.build_graph()

I guess I could do multi-class inheritance and build the second class from all different classes (architectures), but I don't want python to build a huge graph (because I have more than 2 architectures).

Hope it is clear, please tell me if any other information is needed.

Best

Upvotes: 1

Views: 400

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 376052

It sounds like you want composition instead of inheritance. Is an AutoCoder an Architecture? It will work better to make an AutoCoder have an Architecture. Then you can choose how to combine them together.

I don't know your problem domain well enough to make a specific recommendation, but:

class MNISTArchitecture:
    # your code...

class CIFARArchitecture:
    # your code...

class AutoEncoder:
    def __init__(self):
        if condition:
            self.arch = MNISTArchitecture()
        else:
            self.arch = CIFARArchitecture()

Upvotes: 2

Related Questions