Pyrex - __richcmp__

Greg Ewing (using news.cis.dfn.de) ckea25d02 at sneakemail.com
Tue May 6 00:26:57 EDT 2003


Tim Peters wrote:
> [Haris Bogdanovic]
>>How would look operator overloading for operator == :
> 
>    def __eq__(self, other):

Hold your horses, Tim -- he's talking about Pyrex,
not Python!

The correct answer for Pyrex is

   def __richcmp__(self, other, int op):
     if op == 2: # ==
       # return true or false, depending on whether you think
       # self is equal to other

What Tim said about the six relations being independent is
still true, though, so you'll probably want to include a
branch for op == 3 (!=) as well. The best way to write it
is probably

   def __richcmp__(self, other, int op):
     cdef int equal
     equal = ... # whatever you need to do
     if op == 2: # ==
       return equal
     elif op == 3: # !=
       return not equal
     else:
       return NotImplemented

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list