Python 类型错误:__init__() 需要 3 个位置参数,但给出了 4 个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46415700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: __init__() takes 3 positional arguments but 4 were given
提问by MMC
These are the codes for my classes:
这些是我的课程的代码:
class Employee:
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Salary:
def jump(self, name, salary):
print(self.name, self.salary)
class Male(Salary, Employee):
def __init__(self, name, gender, occupation):
super(Male, self).__init__(name, gender, occupation)
self.occupation = occupation
# Separate from all classes (list of instantiated objects)
employee1 = Male("Jim", "male", "technician")
print(Male.name)
When I use the last two lines of the code after creating all my classes, a 'TypeError: init() takes 3 positional arguments but 4 were given' error occurs referencing the super(Male, self).... and employee1 = Male(... lines.
当我使用的代码的最后两行创建所有我的班,“类型错误:之后的init()需要3个位置参数,但4被赋予”发生错误引用超(男,个体经营)......和employee1 =男(……行。
回答by chngzm
Under Pet you have:
在宠物下你有:
def __init__(self, name, color):
self.name = name
self.color = color
Under Dog you have:
在狗下你有:
def __init__(self, name, color, owner):
super(Dog, self).__init__(name, color, owner)
Under Dog there's an extra owner positional argument given, which leads to this error. On a side note, I think super().__init__(name, color)
works just as well too in Python 3
在 Dog 下,给出了一个额外的所有者位置参数,这会导致此错误。附带说明一下,我认为super().__init__(name, color)
在 Python 3 中也能正常工作
回答by cdiaza
class Employee:
def __init__(self,name,gender):
self.name=name
self.gender=gender
class Salary:
def __init__(self,name,gender):
self.name=name
self.gender=gender
def jump(self):
print(self.name,self.salary)
class Male(Salary,Employee):
def __init__(self,name,gender,occupation):
self.occupation=occupation
super().__init__(name,gender)
employee1 = Male("Jim","male","technician")
print(employee1.name)
""" You were writing the occupation parameter in the super, but super calls to the parent class and your parent class does not have occupation parameter. The occupation belongs only to the Class Male."""
"""你在super里写了职业参数,但是super调用了父类,你的父类没有职业参数,这个职业只属于Male这个类。"""
回答by Eyssant
class Employee:
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Salary:
def __init__(self, name, gender):
self.name = name
self.gender = gender
def jump(self):
print(self.name, self.salary)
class Male(Salary, Employee):
def __init__(self, name, gender, occupation):
self.occupation = occupation
Employee.__init__(self, name, gender)
Salary.__init__(self, name, gender)
employee1 = Male("Jim", "male", "technician")
print(employee1.name)
To access all the methods and properties of base class Employee
, super()
function is used in derived class Male
.
Syntax for using super
function is shown in example.
Along with this, use print(employee1.name)
instead of print(Male.name)
.
访问所有的方法和基类的属性Employee
,super()
功能在派生类使用Male
。super
示例中显示了使用函数的语法。与此一起,使用print(employee1.name)
代替print(Male.name)
。