class Student: def __init__(self, name, school, iq): # Attributes of your typical student self.name = name self.school = school self.classes_taken = [] self.iq = iq def greet(self, other_student): ''' Greets other student ''' return "Hi " + other_student.name + \ " my name is " + self.name def take_final(self, class_name, difficulty): ''' Takes final for a class. If student is smart enough, he/she passes the class. ''' if self.iq >= difficulty: print "You passed " + class_name + "!" self.classes_taken.append(class_name) else: print "YOU FAIL!" class MITStudent(Student): def __init__(self, name): # Note here that we use Student's init method and automatically # pass in "MIT" as school and 125 as IQ Student.__init__(self, name, "MIT", 125) def study(self, num_hours): ''' Study for a test, raise your intellect level ''' self.iq += num_hours class HarvardStudent(Student): def __init__(self, name): Student.__init__(self, name, "Hahvahd", 95) ''' Greets other student in the Hahvahdian way ''' def greet(self, other_student): return "Hi " + other_student.name + \ " my name is " + self.name + \ " and I go to Hahvahd." ben = MITStudent("Ben Bitdiddle") jen = Student("Jen Jones", "BU", 100) thadeus = HarvardStudent("Thadeus Winchesterfield the Third") # Students (regardless of whether MITStudent or HarvardStudent or w/e) # can all use the greet method, since they all inherit from the student # class print ben.greet(jen) print jen.greet(ben) print thadeus.greet(ben) # All students can take finals. If passed, the class is added to # the student's classes_taken attribute ben.take_final("5.12", 150) jen.take_final("CS.101", 95) # Since ben is a MITStudent, he can use the class' study method ben.study(30) ben.take_final("5.12", 150) # Jen and Thadeus, on the other hand, can't study # Their study method is not defined, and this would throw an error ##jen.study(10) ##thadeus.study(10)