[Python-Dev] Comparison and (user) confusion

Guido van Rossum guido at python.org
Mon Jul 12 18:34:04 CEST 2004

> There is one good reason for implementing both __cmp__() and rich
> comparisons: when comparing is expensive, it makes sense to call cmp()
> directly if you need the full three-way compare to branch.  (Decimal is
> a good example of this -- at least it was when I originally wrote it,
> and it probably still has the same need now.)

Another good example comes from the <, <=, >, >= operators on lists.
These require you to step through the lists skipping equal item pairs,
and then the comparison outcome is determined by comparing the first
pair of unequal items.  If the item comparison is expensive, it makes
sense to want to compare each pair of items at most once, using cmp().

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list