To determine the rest of the comparison operations, except for __eq__ , it is convenient to use total_ordering . It is enough to define only one method, for example __lt__ , the rest will total_ordering created by total_ordering
from functools import total_ordering @total_ordering class Point: def __init__(self, x, y): self.x = x self.y = y def __lt__(self, other): return self.x < other.x or self.y < other.y def __eq__(self, other): return self.x == other.x and self.y == other.y A = Point(1, 1) B = Point(1, 1) C = Point(2, 1) print(A == B, A < B, A >= B, B <= C) # True False True True
There is also a namedtuple that can do it all.
from collections import namedtuple point = namedtuple('Point', ['x', 'y']) A = point(1, 1) B = point(1, 1) C = point(2, 1) print(A == B, A < B, A >= B, B <= C) # True False True True
def __eq__(self, other): return other.x == self.xSo should it beat? - lalalala