Skip to content

Instantly share code, notes, and snippets.

@pzelnip
Created May 14, 2012 20:23
Show Gist options
  • Save pzelnip/2696496 to your computer and use it in GitHub Desktop.
Save pzelnip/2696496 to your computer and use it in GitHub Desktop.
Define all rich comparison operators in terms of lt
class ComparableMixin:
def __eq__(self, other):
return not (self < other or other < self)
def __le__(self, other):
return not other < self
class Foo(ComparableMixin):
def __init__(self, val):
self.val = val
def __lt__(self, other):
if not isinstance(other, Foo):
raise TypeError('Comparing a Foo to a {}'.format(type(other)))
return self.val < other.val
@pzelnip
Copy link
Author

pzelnip commented May 14, 2012

Duh, I'm an idiot, don't inherit from object, just from ComparableMixin only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment