比较时传入的self和s,是按照上面顺序传入的?
def __cmp__(self, s):
print self.score,s.score
print self.name,s.name
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
else:
if self.name > s.name:
return 1
elif self.name < s.name:
return -1
else:
return 0
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)] print sorted(L) 88 99 Bob Tim 99 88 Alice Bob 99 88 Alice Bob 99 99 Alice Tim [(Alice: 99), (Tim: 99), (Bob: 88)]
上面这段程序优先比较前2个,
第一次比较
为啥self是Bob,s是Tim?
第二次比较
为什么self是Alice,s是Bob
难道是按照字母排序传入self,s?