Separating __cmp__ from < and > ?

Tim Peters tim_one at email.msn.com
Mon Aug 14 20:56:08 EDT 2000


[Huaiyu Zhu]
> In many situations it is conceptually possible to have a!=b without
> either a<b or a>b.  Mathematically, this is the case for objects
> that live in a partially ordered space.
> ...

As you've discovered, since all comparisons in Python today funnel thru a
single cmp function, you can't use the builtin operators to achieve this.

You can define your own (e.g.) lt gt eq ne etc methods, but can't map them
to infix < > == != etc.

Allowing redefinition of individual comparison operators is one of the goals
of David Ascher's "Rich Comparisons" proposal, although the *real* thrust of
that was to allow NumPy to do stuff like return a matrix of boolean values
when doing elementwise matrix compares.

The PEP for Rich Comparisons is at

    http://python.sourceforge.net/peps/pep-0207.html

although it's currently somewhat less than fully fleshed out <ahem>.






More information about the Python-list mailing list