Rich Comparisons

Example: Element-wise list comparsion

 import UserList
 class MyList(UserList.UserList):
     def __eq__(self,other):
         return map(lambda x,y: x==y, self,other)
     def __ne__(self,other):
         return map(lambda x,y: x!=y, self,other)
     def __lt__(self,other):
         return map(lambda x,y: x<y, self,other)
     def __le__(self,other):
         return map(lambda x,y: x<=y, self,other)
     def __gt__(self,other):
         return map(lambda x,y: x>y, self,other)
     def __ge__(self,other):
         return map(lambda x,y: x>=y, self,other)
 
 a = [ 3, 7, 10, -2]
 b = [ 2, 5, 15, 20]
 c = a < b        # c = [0, 0, 1, 1]
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 26
July 26, 2001, beazley@cs.uchicago.edu
>>>