-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctools_total_ordering.py
More file actions
33 lines (24 loc) · 933 Bytes
/
functools_total_ordering.py
File metadata and controls
33 lines (24 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import functools
import inspect
from pprint import pprint
@functools.total_ordering
class MyObject:
def __init__(self, val):
self.val = val
def __eq__(self, other):
print("testing __eq__({}, {})".format(self.val, other.val))
return self.val == other.val
def __gt__(self, other):
print("testing __gt__({}, {})".format(self.val, other.val))
return self.val > other.val
print("Methods:\n")
pprint(inspect.getmembers(MyObject, inspect.isfunction))
a = MyObject(1)
b = MyObject(2)
print("\nComparisons: ")
for expr in ["a < b", "a <= b", "a == b", "a >= b", "a > b"]:
print("\n{:<6}:".format(expr))
result = eval(expr)
print("result of {}: {}".format(expr, result))
# The class must provide implementation of __eq__() and one other rich comparison method. The decorator
# adds implementations of the rest of the methods that work by using the comparisons provided.