Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum set of comparing operators to override

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
like image 988
pt12lol Avatar asked Oct 15 '25 12:10

pt12lol


1 Answers

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.

like image 57
Janne Karila Avatar answered Oct 17 '25 02:10

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!