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

Adam Hupp adam at hupp.org
Wed Oct 17 19:21:18 CEST 2007


On 10/17/07, David A. Wheeler <dwheeler at dwheeler.com> wrote:

class NumberMixinCmp(ComparisonMixin): ... def cmp(self, other): if self.x == other.x: return 0 return (-1 if self.x < other.x else 1)

In the common case the == test will be false. About ~1/2 of the tests will be be <, and half >. It's better then to do:

   if self.x < other.x:
       return -1
   elif self.x > other.x:
       return 1
   else:
       return 0

This almost halves the time difference, as you would expect.

-- Adam Hupp | http://hupp.org/adam/



More information about the Python-3000 mailing list