Skip to content

Instantly share code, notes, and snippets.

@maxwillzq
Created January 7, 2013 15:36
Show Gist options
  • Save maxwillzq/4475885 to your computer and use it in GitHub Desktop.
Save maxwillzq/4475885 to your computer and use it in GitHub Desktop.
#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
@maxwillzq
Copy link
Author

With @functools.total_ordering, User only need define function for ==, >. It will build other operator (<,<=,>= etc) automatically

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