创建SubClass时的TypeError

时间:2015-08-14 18:46:15

标签: python class

我有一个父类定义为:

class Alpha(X, Y, metaclass=abc.ABCMeta):

    def __init__(self, time_series : pandas.Series):
        super(Alpha, self).__init__()

我有它的儿童班:

class Beta(Alpha):
    def __init__(self, returns: [daily_returns_object]):
        super(Beta, self).__init__()
        self.calibrate(returns)

我尝试根据以下内容创建Beta对象:

#returns_list has been defined but is irrelevant here
beta_obj = Beta(returns_list)

我收到错误TypeError:__init__() missing one positional argument: 'time_series'。因此,我写了beta_obj = Beta(time_series, returns_list),但后来得到错误TypeError:__init__() takes 2 positional arguments but 3 were given

1 个答案:

答案 0 :(得分:1)

您在此处调用Alpha.__init__方法:

super(Beta, self).__init__()

但是你没有在那里传递所需的time_series参数。

如果这是Beta也需要的参数,您需要将其添加到Beta.__init__定义中,然后传递给它:

class Beta(Alpha):
    def __init__(self, time_series: pandas.Series, returns: [daily_returns_object]):
        super(Beta, self).__init__(time_series)
        self.calibrate(returns)