I don't trust someone who can't implement `__cmp__` to implement `__lt__` either.
def __lt__(self, other):
return self.a < other.a and self.b < other.b and self.c < other.c
meanwhile def __cmp__(self, other):
v = cmp(self.a, other.a)
if v != 0:
return v
v = cmp(self.b, other.b)
if v != 0:
return v
v = cmp(self.c, other.c)
if v != 0:
return v
return 0
Rust (for instance) makes the latter less offensive (and error-prone) by providing built-in combinators: https://doc.rust-lang.org/std/cmp/enum.Ordering.html impl Ord for Thing {
fn cmp(&self, other: &Self) -> Ordering {
self.a.cmp(&self.a)
.then(self.b.cmp(&other.b))
.then(self.c.cmp(&other.c))
}
}All those functions implement the same comparison, the first is simply not including the similarly trivial implementation for eq:
def __eq__(self, other):
return self.a == other.a and self.b == other.b and self.c == other.c
> you want lexicographic orderingEvery word here makes sense but the objection makes none. What are you trying to say exactly?