python3运行这个报错?
提示这个?
TypeError: '<' not supported between instances of 'Student' and 'Student'
怎么解决?
提示这个?
TypeError: '<' not supported between instances of 'Student' and 'Student'
怎么解决?
2019-10-13
python3需要改一下
import functools 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__ # 重写魔术方法 __cmp__ 排序规则:按照分数从高到底排序,分数相同的按名字排序。 # python3 失效 def my_cmp(self, s): 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, key=functools.cmp_to_key(my_cmp)))
举报