pt1*_*lol 1 python overriding operators
我想在Python中覆盖所有比较运算符(==,!=,<,<=,>,> =),我想尽我所能.从逻辑的角度来看,定义两个任何运算符(不包括对:==和!=,<和> =,>和<=)就足够了.在Python中覆盖这些运算符的最小集合是什么?这样就够了吗?
class MyInt:
__init__(self, num):
self.num = num
__eq__(self, other):
return self.num == other.num
__lt__(self, other):
return self.num < other.num
Run Code Online (Sandbox Code Playgroud)
将functools.total_ordering装饰器应用于您的班级.从它的文档:
这个类必须定义之一
__lt__(),__le__(),__gt__(),或__ge__().此外,该课程应该提供一种__eq__()方法.