python3怎么办? __cmp__还有吗
python3怎么办? __cmp__还有吗
python3怎么办? __cmp__还有吗
2018-10-18
from operator import attrgetter 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__ L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)] print (sorted(L, key=attrgetter('score', 'name'), reverse=True))
官方回答:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
举报