1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#!/usr/bin/python3
from Person import *
class MITPerson(Person):
nextIdNum = 0 # next ID number to assign
def __init__(self, name):
Person.__init__(self, name) # Initialize person's attributes
self.idNum = MITPerson.nextIdNum # MITPerson global attribute: unique ID
MITPerson.nextIdNum += 1
def getIdNum(self):
return self.idNum
# Sorting MIT people uses their ID number, not name
def __lt__(self, other):
return self.idNum < other.idNum
def speak(self, utterance):
return (self.name + " says: " + utterance)
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
def speak(self, utterance):
return MITPerson.speak(self, " Yo Bro: " + utterance)
class Grad(Student):
pass
class TransferStudent(Student):
pass
# By creating a new 'blank' class Student, and making all UG, Grad and
# TransferStudent classes inherit from it, we clean up the class inheritance a
# bit, so, in the function below, we can simply us Student class, to check if an
# object belongs to that class, instead of the old version (commented out below,
# for historical perspective)
def isStudent(obj):
# return isinstance(obj, UG) or isinstance(obj, Grad)
return isinstance(obj, Student)
class Professor(MITPerson):
def __init__(self, name, department):
MITPerson__init__(self, name)
self.department = department
def speak(self, utterance):
new = "In course " + self.department + " we say "
return MITPerson.speak(self, new + utterance)
def lecture(self, topic):
return self.speak('it is obvious that ' + topic)
m1 = MITPerson("Mark Zuckeberg")
m2 = MITPerson("Bill Gates")
m3 = MITPerson("Carlos Maiolino")
p1 = Person("Drew Houston")
s1 = UG("Matt Damon", 2017)
s2 = Grad("Leonardo di Caprio")
# Remember, comparison is done by using __lt__ method, which takes the instance
# itself + another instance. When using the MITPerson instance, the __lt__
# method uses the idNum attribute, which, an instance of Person class will not
# have. But, using an instance of Person class, the __lt__ method, uses the
# LastName, which, since MITPerson is a subclass of Person class, it also
# contains a lastName attribute
## print(p1 < m3) # Comparison works
## print(m3 < p1) # Comparison does not work (Attribute error)
|