A way of structuring code that defines hierarchical relationships between classes.
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
# It is also good practice not to repeat arguments of the parent class
# Arguments *args and **kwargs are here for this purpose
def __init__(self, studentid: int, name: str, firstname: str, age: int, city: str): # The constructor
"""
Constructs a new student object with the specified studentid, name, firstname, age, city.
"""
super().__init__(*args, **kwargs) # 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 " + 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 __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]"