Created
January 7, 2013 15:36
-
-
Save maxwillzq/4475885 to your computer and use it in GitHub Desktop.
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
#Example | |
import functools | |
@functools.total_ordering | |
class Job(object): | |
def __init__(self,priority,descr): | |
self.priority = priority | |
self.descr = descr | |
def __eq__(self,other): | |
print "test __eq__ (%s,%s)" % (self.priority,other.priority) | |
return self.priority == other.priority | |
def __gt__(self,other): | |
print "test __gt__ (%s,%s)" % (self.priority,other.priority) | |
return self.priority>other.priority | |
low = Job(1,'low level') | |
mid = Job(5,'mid level') | |
low2 = Job(1,'2nd low level') | |
print low == low2 | |
print low >= mid | |
print low > mid | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With @functools.total_ordering, User only need define function for ==, >. It will build other operator (<,<=,>= etc) automatically