子类构造函数调用错误__init__

时间:2013-11-13 21:38:04

标签: python

所以我在python中遇到一些继承问题。我有两节课。第一个是

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 ,我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:4)

您无需在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