Rich Comparison Class


/ Published in: Python
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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)

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.