【发布时间】:2020-04-14 11:45:58
【问题描述】:
我有以下课程:
class Word:
def __init__(self, key: str):
self.key = key
self.value = ''.join(sorted(key))
def __lt__(self, other):
if self.value < other.value:
return True
return False
def __gt__(self, other):
if self.value > other.value:
return True
return False
def __eq__(self, other):
val = other.value
if self.value == val:
return True
return False
和
TypeError: '<=' not supported between instances of 'Word' and 'Word'
如何为 python 类覆盖
【问题讨论】:
-
你可能想使用装饰器
functools.total_ordering。
标签: python