Как сохранить форматирование __str__ для класса Student при добавлении студента в пустой список в курсе?< /p>
Код: Выделить всё
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'{self.name} may enrol in this course for the following reasons:\n He is a good looking bloke \n He has matching socks'
class Course:
def __init__(self):
self.course = []
self.eligible_students = ['Michael', 'Gary', 'Travis']
def add_student_to_course(self, student):
if student.name in self.eligible_students:
self.course.append(str(student))
return self.course
def __str__(self):
return f"The course contains these students: {self.course}"
student1 = Student('Michael', 40)
course = Course()
course.add_student_to_course(student1)
print(course)
Подробнее здесь: https://stackoverflow.com/questions/790 ... scape-keys