Created
May 14, 2012 20:23
-
-
Save pzelnip/2696496 to your computer and use it in GitHub Desktop.
Define all rich comparison operators in terms of lt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duh, I'm an idiot, don't inherit from object, just from ComparableMixin only.