gpt4 book ai didi

python - 子类构造函数调用错误的 __init__

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:09 25 4
gpt4 key购买 nike

所以我在 python 中的继承方面遇到了一些麻烦。我有两个类(class)。第一个是

class Base1DHeatEquation:
def __init__(self, alpha, final_time, time_discrete):
self.alpha = alpha
self.final_time = final_time
self.time_discrete = time_discrete

#Additional Functions which aren't causing a problem

第二个是继承第一个的类

class IntialValueTest1DHE(Base1DHeatEquation):
def __init__(self, alpha, final_time, time_discrete,intialValues,\
x_discrete ,noise):
super(IntialValueTest1DHE,self).__init__(self, alpha, final_time, time_discrete)
self.intialValues = intialValues

#Additional Functions which aren't causing a problem

问题是当我尝试创建一个 IntialValueTest1DHE 对象时,我得到以下信息

>>> import HeatEquation1D as he #The File where both classes are stored
>>> temp = he.IntialValueTest1DHE(1,1,100,np.sin,100,0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes exactly 4 arguments (7 given)

似乎对象创建试图从父类而不是子类访问 init,我不确定如何解决这个问题。

最佳答案

您不需要在 super() 声明中添加“self”。此外,您应该使用“新样式”类:

class Base1DHeatEquation(object):
def __init__(self, alpha, final_time, time_discrete):
self.alpha = alpha
self.final_time = final_time
self.time_discrete = time_discrete

class IntialValueTest1DHE(Base1DHeatEquation):
def __init__(self, alpha, final_time, time_discrete,intialValues,
x_discrete ,noise):
super(IntialValueTest1DHE,self).__init__(alpha, final_time, time_discrete)
self.intialValues = intialValues

关于python - 子类构造函数调用错误的 __init__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19964904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com