Rich Comparisons

Example: Complex numbers

 class Complex:
     def __init__(self,r,i):
 	self.real = r
 	self.imag = i
     def __eq__(self,other):
 	if self.real == other.real and \
            self.imag == other.imag: return 1
         return 0
     def __ne__(self,other):
         return not self.__eq__(other)
     def __lt__(self,other):
 	raise TypeError, "can't compare with <, <=, >, >="
     __le__ = Complex.__lt__
     __ge__ = Complex.__lt__
     __gt__ = Complex.__lt__
 
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 25
July 26, 2001, beazley@cs.uchicago.edu
>>>