class Student(object): def __init__(self, name, score): self.name = name self.score = score def __str__(self): return '(%s: %s)' % (self.name, self.score) __repr__ = __str__ def __cmp__(self, s): if False == isinstance(s, Student): #这样判断也行???为什么? return -1 #这里返回-1是什么意思? return -cmp(self.score, s.score) or cmp(self.name, s.name)#这里又怎么解释,这个写法完全不明白! L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)] print sorted(L)
相关分类