Python 3 also removed the “everything is comparable” misfeature of P2, providing way lower incentive to implement ordering: if your type is not orderable, a user knows to go with key functions.
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?
So you want eq, seperate from cmp. Then you still need a way to define ordering, so either write them all for weird cases, or just pick one and have total_ordering do the legwork.
Removing `__cmp__` was a step backwards IMHO.
This again goes back to what masklinn said: a total ordering across all objects in python is a misfeature. The idea that `object() < 13 < "hello world" < 3+2j < [(frozenset(), frozenset())] < {type: int}` should evaluate to anything but a TypeError is horrifying!
And once you make that decision, if an object implements cmp, it must implement eq (because the safe thing should be the default, and allowing people to implement cmp in isolation lets them shoot themselves in the foot). And at that point, cmp is more work to write than le or gt, so instead just implement eq, and then if you want it, le or gt + total_ordering, or if you need weird things implement each method individually.
In practice, I've also found it much easier to reason about eq or le than cmp. People like to be tricky with cmp, and even when they don't, returning -1 or 1 is less explicit than just returning True or False.
At least it evaluates to False.
That's the flaw which cpp avoids, although it remains to be seen if implementing complex spaceships is worth it.
Or I guess, python is now in the pre-cpp20 state, but was never in the post-cpp20 state, so it didn't revert. Python's cmp was broken.
I didn't say that Python was in post-cpp20. Maybe you wanted to comment on my previous comment.
Which is the statement I originally took issue with.