Python中给Employee类添加多个Course实例显示异常求助
问题:添加第二个课程时显示对象内存地址而非课程名称
定义了Course类和Employee类,给Employee实例添加单个Course时能正常显示课程名称,但添加第二个时,输出的是<__main__.Course object at 0x...>这类对象内存地址,而非期望的课程名称列表。
相关代码、当前输出及期望输出如下:
原代码
class Course: def __init__ (self, title, day, start, end): self.title = title self.day = day self.start = start self.end = end def __str__ (self): res = str(self.title) return res class Employee: def __init__(self, firstn, lastn, DoB): self.firstn = firstn self.lastn = lastn self.DoB = DoB self.courses = "no courses" def add_course (self, course): if self.courses == "no courses": self.courses = course else: self.courses = self.courses, course def __str__ (self): res = str(self.firstn) + " " + str(self.lastn) + ", " res += "born " + str(self.DoB) + ", " res += "teaches " + str(self.courses) return res if __name__ == "__main__": print("Create employee 'John Doe', born January 1st, 1990") john = Employee('John', 'Doe', '1990-01-01') print(' Created:', john) print("Create employee 'Jane Doe', born August 10, 1992") jane = Employee('Jane', 'Doe', '1992-08-10') print(' Created:', jane) print("Add 'Python I' to John Doe's courses") python1 = Course('Python I', 'Tuesday', 12, 14) john.add_course(python1) print(' John:', john) print("Add 'Introduction to Semantics' to John Doe's courses") semantics = Course('Introduction to Semantics', 'Monday', 10, 12) john.add_course(semantics) print(' John:', john) print("Add 'Python I' to Jane Doe's courses") jane.add_course(python1) print(' Jane:', jane)
当前输出
Create employee 'John Doe', born January 1st, 1990 Created: John Doe, born 1990-01-01, teaches no courses Create employee 'Jane Doe', born August 10, 1992 Created: Jane Doe, born 1992-08-10, teaches no courses Add 'Python I' to John Doe's courses John: John Doe, born 1990-01-01, teaches Python I Add 'Introduction to Semantics' to John Doe's courses John: John Doe, born 1990-01-01, teaches (<__main__.Course object at 0x0000029B89DF0E10>, <__main__.Course object at 0x0000029B89DF0E50>) Add 'Python I' to Jane Doe's courses Jane: Jane Doe, born 1992-08-10, teaches Python I
期望输出
John: John Doe, born 1990-01-01, teaches Python I, Introduction to Semantics
解决方案
问题根源在add_course方法:你将courses从单个Course对象转为了元组,而元组的默认字符串表示会显示元素的repr(即内存地址格式),而非调用每个元素的__str__。后续添加课程还会生成嵌套元组,逻辑更混乱。
正确做法是用列表统一管理课程,修改如下:
1. 修改Employee初始化逻辑
将self.courses = "no courses"改为空列表,统一用列表存储课程:
def __init__(self, firstn, lastn, DoB): self.firstn = firstn self.lastn = lastn self.DoB = DoB self.courses = [] # 初始化空列表表示无课程
2. 简化add_course方法
直接向列表追加课程,无需判断初始状态:
def add_course(self, course): self.courses.append(course)
3. 调整Employee的__str__方法
根据列表是否为空,生成对应的显示文本:
def __str__(self): res = f"{self.firstn} {self.lastn}, born {self.DoB}, teaches " if not self.courses: res += "no courses" else: res += ", ".join(str(course) for course in self.courses) return res
修改后的完整代码
class Course: def __init__ (self, title, day, start, end): self.title = title self.day = day self.start = start self.end = end def __str__ (self): return str(self.title) class Employee: def __init__(self, firstn, lastn, DoB): self.firstn = firstn self.lastn = lastn self.DoB = DoB self.courses = [] def add_course(self, course): self.courses.append(course) def __str__(self): res = f"{self.firstn} {self.lastn}, born {self.DoB}, teaches " if not self.courses: res += "no courses" else: res += ", ".join(str(course) for course in self.courses) return res if __name__ == "__main__": print("Create employee 'John Doe', born January 1st, 1990") john = Employee('John', 'Doe', '1990-01-01') print(' Created:', john) print("Create employee 'Jane Doe', born August 10, 1992") jane = Employee('Jane', 'Doe', '1992-08-10') print(' Created:', jane) print("Add 'Python I' to John Doe's courses") python1 = Course('Python I', 'Tuesday', 12, 14) john.add_course(python1) print(' John:', john) print("Add 'Introduction to Semantics' to John Doe's courses") semantics = Course('Introduction to Semantics', 'Monday', 10, 12) john.add_course(semantics) print(' John:', john) print("Add 'Python I' to Jane Doe's courses") jane.add_course(python1) print(' Jane:', jane)
修改后的输出
Create employee 'John Doe', born January 1st, 1990 Created: John Doe, born 1990-01-01, teaches no courses Create employee 'Jane Doe', born August 10, 1992 Created: Jane Doe, born 1992-08-10, teaches no courses Add 'Python I' to John Doe's courses John: John Doe, born 1990-01-01, teaches Python I Add 'Introduction to Semantics' to John Doe's courses John: John Doe, born 1990-01-01, teaches Python I, Introduction to Semantics Add 'Python I' to Jane Doe's courses Jane: Jane Doe, born 1992-08-10, teaches Python I
内容的提问来源于stack exchange,提问作者user11579493
相关产品推荐
相关产品推荐

