Student inherits from Person
Create a Student class that inherits from Person, adds student_id and grades, and prints name, age and id.
Ravi, 17, ID S-104
Approach
- Call
super().__init__for the shared attributes.
Solution
class Person: def __init__(self, name, age): self.name, self.age = name, age class Student(Person): def __init__(self, name, age, student_id, grades): super().__init__(name, age) self.student_id = student_id self.grades = grades def show(self): print(f"{self.name}, {self.age}, ID {self.student_id}") Student("Ravi", 17, "S-104", [88, 92]).show()Ravi, 17, ID S-104
Line by line
The parent sets name and age, so that code is not repeated.
Learn the concept
New to this topic? Read the lesson, then come back.