【发布时间】:2020-09-24 02:00:59
【问题描述】:
person.py
class Person:
"""---A class representing a person---"""
# Person constructor
def __init__(self,n,a):
self.full_name = n
self.age = a
class Student(Person):
# Student constructor
def __init__(self,n,a,s):
Person.__init__(self,n,a)
self.school = s
driver.py
from person import *
a = Student("Alice", 19, "Univ")
它抛出TypeError: __init__() takes 3 positional arguments but 4 were given
我尝试将 Student 类更改为以下内容:
class Student(Person):
# Student constructor
def __init__(self,n,a,s):
super().__init__(n,a)
self.school = s
错误依然存在。
为什么会这样?是否需要 super() 关键字才能添加新属性?
编辑:问题解决了。呈现这种奇怪行为的源代码中存在缩进问题,因此应该关闭问题。
【问题讨论】:
标签: python inheritance parent-child subclass super