一个类可以继承一个"参数"类? (有条件的继承)

时间:2017-12-18 02:01:40

标签: python class inheritance

我正在使用python 3.6,并希望从不同的可能类中构建一个子类。

所以玩具的例子是:

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

然后构建一个可以从第一个架构或第二个架构继承的类:

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

我想我可以做多类继承并从所有不同的类(架构)构建第二个类,但我不想让python构建一个巨大的图(因为我有超过2个架构)。

希望很清楚,如果需要任何其他信息,请告诉我。

最佳

1 个答案:

答案 0 :(得分:2)

听起来你想要的是构图而不是继承。 AutoCoder是架构吗?使AutoCoder具有架构会更好。然后你可以选择如何将它们组合在一起。

我不太了解您的问题域,无法提出具体建议,但是:

class MNISTArchitecture:
    # your code...

class CIFARArchitecture:
    # your code...

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