派生类构造函数在 python 中如何工作?

发布于 2024-08-14 00:33:08 字数 1006 浏览 4 评论 0原文

我有以下基类:

class NeuralNetworkBase:
    def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
        self.inputLayer = numpy.zeros(shape = (numberOfInputs))
        self.hiddenLayer = numpy.zeros(shape = (numberOfHiddenNeurons))
        self.outputLayer = numpy.zeros(shape = (numberOfOutputs))

        self.hiddenLayerWeights = numpy.zeros(shape = (numberOfInputs, numberOfHiddenNeurons))
        self.outputLayerWeights = numpy.zeros(shape = (numberOfHiddenNeurons, numberOfOutputs))

现在,我有一个具有以下代码的派生类:

class NeuralNetworkBackPropagation(NeuralNetworkBase):
    def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
        self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
        self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))

但是当我实例化 NeuralNetworkBackPropagation 时,我希望两个构造函数都被调用。也就是说,我不想重写基类的构造函数。 python 在运行派生类时默认调用基类构造函数吗?我是否必须在派生类构造函数中隐式执行此操作?

I have the following base class:

class NeuralNetworkBase:
    def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
        self.inputLayer = numpy.zeros(shape = (numberOfInputs))
        self.hiddenLayer = numpy.zeros(shape = (numberOfHiddenNeurons))
        self.outputLayer = numpy.zeros(shape = (numberOfOutputs))

        self.hiddenLayerWeights = numpy.zeros(shape = (numberOfInputs, numberOfHiddenNeurons))
        self.outputLayerWeights = numpy.zeros(shape = (numberOfHiddenNeurons, numberOfOutputs))

now, I have a derived class with the following code:

class NeuralNetworkBackPropagation(NeuralNetworkBase):
    def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
        self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
        self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))

But when I instantiate NeuralNetworkBackPropagation I'd like that both constructors get called.This is, I don't want to override the base class' constructor. Does python call by default the base class constructor's when running the derived class' one? Do I have to implicitly do it inside the derived class constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧梦荧光笔 2024-08-21 00:33:08

Python默认调用基类吗
运行时的类构造函数
派生类'一?我必须吗
在派生中隐式执行此操作
类构造函数?

不,又是。

这与 Python 处理其他重写方法的方式一致 - 如果您希望在继承类中使用该功能,则必须显式调用已重写的基类中的任何方法。

您的构造函数应如下所示:

def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
    NeuralNetworkBase.__init__(self, numberOfInputers, numberOfHiddenNeurons, numberOfOutputs)
    self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
    self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))

或者,您可以使用 Python 的 super 函数可以实现同样的效果,但是使用时需要小心

Does python call by default the base
class constructor's when running the
derived class' one? Do I have to
implicitly do it inside the derived
class constructor?

No and yes.

This is consistent with the way Python handles other overridden methods - you have to explicitly call any method from the base class that's been overridden if you want that functionality to be used in the inherited class.

Your constructor should look something like this:

def __init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs):
    NeuralNetworkBase.__init__(self, numberOfInputers, numberOfHiddenNeurons, numberOfOutputs)
    self.outputLayerDeltas = numpy.zeros(shape = (numberOfOutputs))
    self.hiddenLayerDeltas = numpy.zeros(shape = (numberOfHiddenNeurons))

Alternatively, you could use Python's super function to achieve the same thing, but you need to be careful when using it.

〃安静 2024-08-21 00:33:08

您必须将其放在 NeuralNetworkBackPropagation 的 __init__() 方法中,即调用父类(NeuralNetworkBase)的 __init__() 方法

NeuralNetworkBase.__init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs)

:除非您在子类中覆盖它,否则始终会自动调用父类。如果您在子类中覆盖它并且还想调用父类的构造函数,那么您必须按照我上面所示的方式进行操作。

You will have to put this in the __init__() method of NeuralNetworkBackPropagation, that is to call the __init__() method of the parent class (NeuralNetworkBase):

NeuralNetworkBase.__init__(self, numberOfInputs, numberOfHiddenNeurons, numberOfOutputs)

The constructor of the parent class is always called automatically unless you overwrite it in the child class. If you overwrite it in the child class and want to call the parent's class constructor as well, then you'll have to do it as I showed above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文