strange behavior of the comparison operator

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Aug 29 12:35:42 EDT 2001


On Wed, 29 Aug 2001, Nicolas Evrard wrote:

> Hello everyone, this is my first post to the list and let me say that I
> find python really exciting ... Looks a bit like scheme but is fast and
> have a large community of users ...
>
> Here 's what I've done :
>
> To try the __comp__ method I've done a class for complex number (the usual
> example). But here are the result I've got :
>
> >>> Complex(7,2).__comp__(Complex(3,4))
> 1
> >>> Complex(7,2) < Complex(3,4)
> 1
>
> the definition of __comp__ I've done :
>
>     def __comp__(self, other) :
>         if self.r < other.r :
>             return -1
>         elif self.r == other.r :
>             if self.i < other.i :
>                 return -1
>             elif self.i == other.i : return 0
>         else : return 1
>
> But in the reference manual it says that __comp__ should return a positive
> integer if self > other ... and obviously it isn't the case.

Mmm. The question becomes "When do you consider one complex number to be
greater or less than another?" Answer that question first and you will find
your solution.

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list