Separating __cmp__ from < and > ?
Rainer Deyke
root at rainerdeyke.com
Mon Aug 14 23:52:15 EDT 2000
"Erik Max Francis" <max at alcyone.com> wrote in message
news:39989C42.AC6D8C9B at alcyone.com...
> In my case, I had a class which encapsulated (essentially) an ordered
> pair, which sounds like a similar situation to yours. I wanted to test
> for equality and inequality, but inequalities have little meaning. So I
> simply defined a __cmp__ method that does something meaningful on
> equalities and inequalities, and does something arbitrary on
> inequalities. In this case it was simple running cmp on tuples
> constructed from the two coordinates:
>
> def __cmp__(self, other):
> """Not very meaningful for inequalities."""
> return cmp(self.tuple(), other.tuple())
This is probably the best approach. In Python, all objects are well ordered
by default. If the interpreter knows whether type([]) is greater or less
than 5, then there is really no excuse for objects of the same class not
being well ordered.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list