python3.x 的问题
python3.x 中取消了cmp这个用法,那这个题在python3.x中该怎么写啊?有没有大神可以指点一下
python3.x 中取消了cmp这个用法,那这个题在python3.x中该怎么写啊?有没有大神可以指点一下
2017-07-15
python3 的 operator 可以 但返回 正相反
In [46]: 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 __operator__(self, s):
...: if self.score == s.score:
...: return operator(self.name, s.name)
...: return -operator(self.score, s.score)
...:
...: L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]
...: print sorted(L)
...:
...:
[(Bob: 88), (Tim: 99), (Alice: 99)]
举报