I would like to override all comparing operators (==, !=, <, <=, >, >=) in Python and I would like to do the least I can. In logic point of view it is enough to define two any operators (excluding pairs: == and !=, < and >=, > and <=). What is the minimum set of these operators to override in Python? Is it for example enough?
class MyInt:
__init__(self, num):
self.num = num
__eq__(self, other):
return self.num == other.num
__lt__(self, other):
return self.num < other.num
Apply the functools.total_ordering
decorator to your class. From its docs:
The class must define one of
__lt__()
,__le__()
,__gt__()
, or__ge__()
. In addition, the class should supply an__eq__()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With