_init_() takes 4 positional arguments but 5 were given
源代码是这样
class people:
name = ''
age = 0
__weight = 0
def __init__(self, n, a ,w):
self.name = n
self.age = a
self.weight = w
def speak(self):
print("hello, I'm {}, I'm {} years old".format(self.name, self.age))
class student(people):
grade = ''
def __init(self, n, a, w, g):
people.__init__(self, n, a, w)
self.grade = g
def speak(self):
print("hello, I'm {}, I'm {} years old, I'm in {} grade".format(self.name, self.age, self.grade))
s = student('yang', 23, 60, 9)
s.speak()
有报错
改了半天没改明白,后来发现是构造函数后面的下划线没写。是个非常小的错误
把下划线加上就可以了
def __init__(self, n, a, w, g):
people.__init__(self, n, a, w)
self.grade = g