A way of structuring code that defines hierarchical relationships between classes.
from person import Person
class Student(Person):
"""Represents a student of IMT Atlantique."""
# It is good practice to also call the parent's constructor
# Then you can complement with additional codes if needed
def __init__(self, studentid: int, name: str, firstname: str, age: int, city: str) -> None: # The constructor
"""Constructs a new student object with the specified studentid, name, firstname, age, and city."""
super().__init__(name, firstname, age, city) # Call parent's constructor
self._studentid: int = studentid
self._courses: list[str] = []
def to_string(self) -> str:
"""Returns a string representation of the person."""
return (
f"{self._studentid}, " + super().to_string() + ". Enrolled in " + str(self._courses)
)
def enroll_in_course(self, course_name):
"""Add the course_name in the list of courses of the student."""
if course_name not in self._courses:
self._courses.append(course_name)
if __name__ == "__main__":
one_student = Student(1234, "Weber", "Alice", 33, "London")
print(
one_student.to_string()
) # Expected output: "1234, Alice Weber (33 old, from London). Enrolled in []"
one_student.enroll_in_course("UE Informatique")
print(
one_student.to_string()
) # Expected output: "1234, Alice Weber (33 old, from London). Enrolled in [UE Informatique]"