一个类可以从“参数”类继承吗? (有条件的继承)

问题描述 投票:1回答:1

我正在使用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个架构)。

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

最好

python class inheritance
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.