Visualizing the Hierarchy
- sort instance through __lt__
#sort people,return True if self's name is lexicographically
less than other's name, and False otherwise
def __lt__(self, other):
if self.lastName == other.lastName:
return self.name < other.name
return self.lastName < other.lastName
# sorting MIT people uses their ID number, not name!
def __lt__(self, other):
return self.idNum < other.idNum
2.不同层级的class之间的比较
p1 = MITPerson(‘Eric’)
p2 = MITPerson(‘John’)
p3 = MITPerson(‘John’)
p4 = Person(‘John’)
现象:
p1 < p4 Attribute Error
p4 < p1 False
原因: