Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 多重继承:接受0个位置参数,但提供了1个_Python 3.x_Multiple Inheritance - Fatal编程技术网

Python 3.x 多重继承:接受0个位置参数,但提供了1个

Python 3.x 多重继承:接受0个位置参数,但提供了1个,python-3.x,multiple-inheritance,Python 3.x,Multiple Inheritance,在测试多重继承时,我有以下Date、Time和DateTime类继承权 class time: def __init__(self, time): self.time = time def getTime(): return self.time; class date: def __init__(self, date): self.date = date def getDate(): retu

在测试多重继承时,我有以下Date、Time和DateTime类继承权

class time:
    def __init__(self, time):
        self.time = time 

    def getTime():
        return self.time;

class date:
    def __init__(self, date):
        self.date = date

    def getDate():
        return self.date

class datetime(time,date):
    def __init__(self, input_time, input_date):
        time.__init__(self, input_time)
        date.__init__(self, input_date)
实例化和检查日期工作正常:

my_datetime = datetime("12PM","Today")
my_datetime.date
但是运行getDate函数会导致参数错误,我不明白为什么

my_datetime.getDate()
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 my_datetime.getDate()
TypeError:getDate()接受0个位置参数,但提供了1个

您的问题与多重继承问题无关。事实上,在
date
的实例上调用
getDate
时,您会遇到完全相同的错误


问题的原因是您忘记将
self
列为
getDate
(以及
time.getTime
)的参数。调用该方法的实例将自动作为第一个位置参数传递,因此您需要记住这一点来编写该方法。

错误告诉您出了什么问题。您已将
getDate
定义为不接受任何参数。执行
someObject.someMethod()
时,python会自动将对象实例作为第一个参数传递(几乎通用名为
self

如果应在类的实例上调用
getDate
,则需要如下定义:

def getDate(self):
    ...

应该是
def getDate(self):
因为Python在调用
my_datetime.getDate()
@PatrickHaugh时内部传递
self
,我以前看过这个,但没有注意到区别,但我第二次看到它。谢谢
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-120ecf58a608> in <module>
----> 1 my_datetime.getDate()

TypeError: getDate() takes 0 positional arguments but 1 was given
def getDate(self):
    ...