Python TypeError:\uuuu init\uuuu()接受4个位置参数,但给出了7个
我无法找到问题的解决方案,也无法理解为什么会发生此错误。我是python新手。谢谢您的scientist类的init函数写为:Python TypeError:\uuuu init\uuuu()接受4个位置参数,但给出了7个,python,multiple-inheritance,Python,Multiple Inheritance,我无法找到问题的解决方案,也无法理解为什么会发生此错误。我是python新手。谢谢您的scientist类的init函数写为: class Employee(object): def __init__(self,ename,salary,dateOfJoining): self.ename=ename self.salary=salary self.dateOfJoining=dateOfJoining def output(sel
class Employee(object):
def __init__(self,ename,salary,dateOfJoining):
self.ename=ename
self.salary=salary
self.dateOfJoining=dateOfJoining
def output(self):
print("Ename: ",self.ename,"\nSalary: ",self.salary,"\nDate Of
Joining: ",self.dateOfJoining)
class Qualification(object):
def __init__(self,university,degree,passingYear):
self.university=university
self.degree=degree
self.passingYear=passingYear
def qoutput(self):
print("Passed out fom University:",self.university,"\nDegree:",self.degree,"\Passout year: ",self.passingYear)
class Scientist(Employee,Qualification):
def __int__(self,ename,salary,dateOfJoining,university,degree,passingYear):
Employee.__init__(self,ename,salary,dateOfJoining)
Qualification.__init__(self,university,degree,passingYear)
def soutput(self):
Employee.output()
Qualification.output()
a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')
a.soutput()
而不是
def __int__
因此,它从父类继承了init函数,父类接收的参数比发送给该类的参数少
另外,您应该使用super函数,而不是调用父函数的init
def __init__
这显然适用于所有父函数。您的科学家类的init函数写为:
class Employee(object):
def __init__(self,ename,salary,dateOfJoining):
self.ename=ename
self.salary=salary
self.dateOfJoining=dateOfJoining
def output(self):
print("Ename: ",self.ename,"\nSalary: ",self.salary,"\nDate Of
Joining: ",self.dateOfJoining)
class Qualification(object):
def __init__(self,university,degree,passingYear):
self.university=university
self.degree=degree
self.passingYear=passingYear
def qoutput(self):
print("Passed out fom University:",self.university,"\nDegree:",self.degree,"\Passout year: ",self.passingYear)
class Scientist(Employee,Qualification):
def __int__(self,ename,salary,dateOfJoining,university,degree,passingYear):
Employee.__init__(self,ename,salary,dateOfJoining)
Qualification.__init__(self,university,degree,passingYear)
def soutput(self):
Employee.output()
Qualification.output()
a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')
a.soutput()
而不是
def __int__
因此,它从父类继承了init函数,父类接收的参数比发送给该类的参数少
另外,您应该使用super函数,而不是调用父函数的init
def __init__
这显然适用于所有父函数。您的Scientist类构造函数拼写错误为
\uuuu int\uuuu
,而不是\uuu init\uuu
。由于Scientist类中没有要使用的重写构造函数,因此它在继承链上上升了一个级别,并使用Employee的构造函数,而Employee的构造函数实际上只使用4个位置参数。只要修改一下打字错误,它就应该可以工作了
(您在代码中对类还做了一些其他不好的事情,但我允许其他人用提示进行评论)您的Scientist类构造函数拼写错误为
\uuuu int\uuuu
,而不是\uu init\uuu
。由于Scientist类中没有要使用的重写构造函数,因此它在继承链上上升了一个级别,并使用Employee的构造函数,而Employee的构造函数实际上只使用4个位置参数。只要修改一下打字错误,它就应该可以工作了
(代码中的类还有其他一些不好的地方,但我允许其他人用提示进行评论)发生错误的是哪个类?您能给我们完整的异常跟踪吗?对于Scientist类,请发布显示错误发生所在行的python回溯。@tdelaney这里有回溯(最近一次调用):文件“C:/Python34/84.py”,第22行,在a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')类型错误:uu init接受4个位置参数,但提供了7个。
\uuuu init\uuuu
中有输入错误。您编写了\uuuu int\uuuu
。它使用父类的dunder init,错误发生在哪个类上?您能给我们完整的异常跟踪吗?对于Scientist类,请发布显示错误发生所在行的python回溯。@tdelaney这里有回溯(最近一次调用):文件“C:/Python34/84.py”,第22行,在a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')类型错误:uu init接受4个位置参数,但提供了7个。\uuuu init\uuuu
中有输入错误。您编写了\uuuu int\uuuu
。它使用父类的dunder init