[Python-3000] Please re-add cmp to python 3000 (original) (raw)

Adam Olsen rhamph at gmail.com
Wed Oct 31 06:53:02 CET 2007


On 10/30/07, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

Adam Olsen wrote: > for a, b in zip(self.data, other.data): > result = richcmp(a, b, ordered) > if result: > return result

That can't be right, because there are three possible results you need to be able to distinguish from comparing a pair of elements: "stop and return True", "stop and return False", and "keep going". There's no way you can get that out of a boolean return value.

It's not strictly a boolean value.

If ordered is false then you interpret it as either a false value or a true value (but it may return -1 or +1 for the true values.)

If ordered is true then it may be -1, 0/false, +1, or raise a TypeError if ordering is unsupported.

def cmp(self, other): for a, b in zip(self.items, other.items): result = cmp(a, b) if result != 0: return result return 0

Which is actually the same as it is now, with an added bit of It Just Works behaviour: if any of the element comparisons gives UnequalButNotOrdered, then the whole sequence gets reported as such.

So the difference between our two approaches is that mine uses a flag to indicate if a TypeError should be raised, while yours adds an extra return value.

Mine does have a small benefit: list currently exits early if it's only testing for equality and the lengths differ, which couldn't be done with your API.

-- Adam Olsen, aka Rhamphoryncus



More information about the Python-3000 mailing list