###这个__str__方法让人疑惑啊 ###当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据 __str__方法需要返回一个字符串,当做这个对象的描写 class student: def __init__(self, name , num, score, homework): self.name= name self.num= num self.score=score self.homework= homework self.cnt = len(homework) def detail(self): print(self.name+'\'s student number is '+self.num+', and his grade is '+self.score+'. He submitted '+ str(self.cnt)+' assignments, each with a grade of '+" ".join(self.homework)) obj = student(input(),input(),input(),input().split()) obj.detail() return "{}'s student number is {}, and his grade is {}. He submitted {} assignments, each with a grade of {}".format(self.name,self.number,self.grade,self.times,self.level) class Student: def __init__(self, name, stu_num, score, grade): self.name = name self.stu_num = stu_num self.score = score self.grade = grade #__str__方法用于返回对象的描述信息,如果不使用__str__方法,直接print,或者return,返回的是对象的内存地址。 def __str__(self): return ("%s's student number is %s, and his grade is %d. He submitted %s assignments, each with a grade of %s" % (self.name, self.stu_num, int(self.score), len(self.grade.split()), self.grade)) name1 = input() stu_num1 = input() score1 = input() grade1 = input() stu = Student(name1, stu_num1, score1, grade1) print(stu)