派生类构造函数在 python 中如何工作?
我有以下基类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,又是。
这与 Python 处理其他重写方法的方式一致 - 如果您希望在继承类中使用该功能,则必须显式调用已重写的基类中的任何方法。
您的构造函数应如下所示:
或者,您可以使用 Python 的
super
函数可以实现同样的效果,但是使用时需要小心。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:
Alternatively, you could use Python's
super
function to achieve the same thing, but you need to be careful when using it.您必须将其放在 NeuralNetworkBackPropagation 的 __init__() 方法中,即调用父类(NeuralNetworkBase)的 __init__() 方法
:除非您在子类中覆盖它,否则始终会自动调用父类。如果您在子类中覆盖它并且还想调用父类的构造函数,那么您必须按照我上面所示的方式进行操作。
You will have to put this in the
__init__()
method of NeuralNetworkBackPropagation, that is to call the__init__()
method of the parent class (NeuralNetworkBase):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.