这一段是怎么比较分数的??小白一个请教
def __cmp__(self, s):
if self.score==s.score:
return cmp (self.name,s.name)
return -cmp (self.score,s.score)
def __cmp__(self, s):
if self.score==s.score:
return cmp (self.name,s.name)
return -cmp (self.score,s.score)
2018-11-12
你把代码这样写:
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 self.score == s.score:
return cmp(self.name,s.name)
elif self.score < s.score:
return 1
elif self.score > s.score:
return -1
L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
print sorted(L)
这样好理解些,给出的return -cmp(self.score,s,score)我也没有理解是什么意思
举报