1

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

  • 1
    do want all instances of `AutoEncoder` to inherit from the same class based on a condition one time, or do you want to be able to set the parent class for each instance? – sam-pyt Dec 18 '17 at 02:11
  • Thanks for your help, I would like to use the relevant architecture depending on the dataset name (mnist, cifar10) argument in the bigger main function. I guess it would be the second option. – houssamzenati Dec 18 '17 at 02:21

1 Answers1

2

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()
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662