作用:在我们比较两个对象时,如果是str,int那sorted()就用cmp默认函数进行比较,那如果是两个实例对象,就只能使用__cmp__()自订方法进行比较
class Student:
def __init__(self,name,score):
self.name = name
self.score = score
def __cmp__(self,another):
if self.name < another.name:
return -1
elif self.name > another.name:
return 1
else:
return 0
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)