Your Ad Here

Posted By

djbobyd on 09/18/11


Tagged


Versions (?)

Rich Comparison Class


 / Published in: Python
 

URL: http://www.voidspace.org.uk/python/articles/comparison.shtml

Meta Class that can be used for simplifying rich comparison in Python

  1. class RichComparisonMixin(object):
  2.  
  3. def __eq__(self, other):
  4. raise NotImplementedError("Equality not implemented")
  5.  
  6. def __lt__(self, other):
  7. raise NotImplementedError("Less than not implemented")
  8.  
  9. def __ne__(self, other):
  10. return not self.__eq__(other)
  11.  
  12. def __gt__(self, other):
  13. return not (self.__lt__(other) or self.__eq__(other))
  14.  
  15. def __le__(self, other):
  16. return self.__eq__(other) or self.__lt__(other)
  17.  
  18. def __ge__(self, other):
  19. return self.__eq__(other) or self.__gt__(other)

Report this snippet  

You need to login to post a comment.