首页 > 钓鱼视频 > 高手钓鱼视频

6.00笔记14(我的小猫钓鱼)

高手钓鱼视频钓友圈2023-08-10 23:43:04A+A-
>>> class Person(object): def __init__(self, name): self.name = name try: lastBlank = name.rindex() self.lastName = name[lastBlank+1:] except: self.lastName = name self.birthday = None def getName(self): return self.name def getLastName(self): return self.lastName def setBirthday(self, birthdate): self.birthday = birthdate def getAge(self): if self.birthday == None: raise ValueError return (datetime.date.today() - self.birthday).days def __lt__(self, other): if self.lastName == other.lastName: return self.name < other.name return self.lastName < other.lastName def __str__(self): return self.name >>> class MITPerson(Person): nextIdNum = 0 def __init__(self, name): Person.__init__(self, name) self.idNum = MITPerson.nextIdNum MITPerson.nextIdNum += 1 def getIdNum(self): return self.idNum def __lt__(self, other): return self.idNum < other.idNum >>> class Student(MITPerson): pass >>> class UG(Student): def __init__(self, name, classYear): MITPerson.__init__(self, name) self.year = classYear def getClass(self): return self.year >>> class Grad(Student): pass >>> class Grades(object): def __init__(self): self.students = [] self.grades = {} self.isSorted = True def addStudent(self, student): # Assumes: student is of type Student if student in self.students: raise ValueError(Duplicate student) self.students.append(student) self.grades[student.getIdNum()] = [] self.isSorted = False def addGrade(self, student, grade): try: self.grades[student.getIdNum()].append(grade) except: raise ValueError(Student not in mapping) def getGrades(self, student): try: return self.grades[student.getIdNum()][:] except: raise ValueError(Student not in mapping) def getStudents(self): if not self.isSorted: self.students.sort() self.isSorted = True return self.students[:] >>> def gradeReport(course): """Assumes course is of type Grades""" report = for s in course.getStudents(): tot = 0.0 numGrades = 0 for g in course.getGrades(s): tot += g numGrades += 1 try: average = tot / numGrades report = report + \n\ + str(s) + \s mean grade is + str(average) except ZeroDivisionError: report = report + \n\ + str(s) + has no grades return report >>> ug1 = UG(Jand Doe, 2014) >>> ug2 = UG(Jonh Doe, 2015) >>> ug3 = UG(David Henry, 2003) >>> g1 = Grad(Billy Buckner) >>> g2 = Grad(Bucky F.Dent) >>> sixHundred = Grades() >>> sixHundred.addStudent(ug1) >>> sixHundred.addStudent(ug2) >>> sixHundred.addStudent(g1) >>> sixHundred.addStudent(g2) >>> for s in sixHundred.getStudents(): sixHundred.addGrade(s, 75) >>> sixHundred.addGrade(g1, 25) >>> sixHundred.addGrade(g2, 100) >>> sixHundred.addStudent(ug3) >>> print (gradeReport(sixHundred)) Jand Does mean grade is 75.0 Jonh Does mean grade is 75.0 David Henry has no grades Billy Buckners mean grade is 50.0 Bucky F.Dents mean grade is 87.5

这是我学python以来最长的一个代码,让我想到我小学三年级的时候学的小猫钓鱼的文章。我的语文老师要求每篇课文都要背下来,因此小猫钓鱼这篇文章我至今印象深刻,因为当时觉得实在是太长了。转眼间,我的儿子也开始学小猫钓鱼了。我一看,咦?怎么这么短?

同样的,这个代码我也花了好几天功夫,断断续续才搞懂。希望过段时间回头看,也能有同样的惊叹,咦?这么短?

书归正传。

这里分成两个大类,一个是class Person,另一个是class Grades。

在class Grades里面,addStudent()函数中,其中一个参数是class Person下面一个子类(class Student)的实例。这样就把两个大类串联起来了,由此可以在class Grades里面调用class Person及子类的函数,比如:

self.grades[student.getIdNum()] = []

类似的方法,在gradeReport()函数中,参数又是Grades的实例,因此又可以在函数中调用class Grades里面的函数,比如:

for s in course.getStudents():

之前一直好奇,class最后返回什么。现在看来,class并不返回什么。但class有特定的对象和匹配的方法,每个方法可以返回不同的值,而函数可以调用实例和方法,利用这些值搞一些事情。

点击这里复制本文地址 以上内容由趣钓网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

相关内容

模板文件不存在: ./template/plugins/comment/pc/index.htm

Copyright © 2012-2024 趣钓网 版权所有

钓鱼比赛视频 | 高手钓鱼视频 | 海钓视频 | 台钓视频 | XML地图 | HTML地图

qrcode
返回顶部
X趣钓网

截屏,微信识别二维码

微信号:

(点击微信号复制,添加好友)

  打开微信

微信号已复制,请打开微信添加咨询详情!