Advertisement
rfmonk

functools_total_ordering.py

Jan 15th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import functools
  5. import inspect
  6. from pprint import pprint
  7.  
  8.  
  9. @functools.total_ordering
  10. class MyObject(object):
  11.     def __init__(self, val):
  12.         self.val = val
  13.  
  14.     def __eq__(self, other):
  15.         print ' testing __eq__(%s, %s)' % (self.val, other.val)
  16.         return self.val == other.val
  17.  
  18.     def __gt__(self, other):
  19.         print ' testing __gt__(%s, %s)' % (self.val, other.val)
  20.         return self.val > other.val
  21.  
  22. print 'Methods:\n'
  23. pprint(inspect.getmembers(MyObject, inspect.ismethod))
  24.  
  25. a = MyObject(1)
  26. b = MyObject(2)
  27.  
  28. print '\nComparisons:'
  29. for expr in ['a < b', 'a <= b', 'a == b', 'a >= b', 'a > b']:
  30.     print '\n%-6s:' % expr
  31.     result = eval(expr)
  32.     print ' result of %s: %s' % (expr, result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement