1 | from __future__ import print_function |
---|
2 | from functools import total_ordering |
---|
3 | from itertools import product |
---|
4 | import sys |
---|
5 | |
---|
6 | print(sys.version) |
---|
7 | |
---|
8 | #for case in ['no_cmp', 'cmp', 'own_cmp']: |
---|
9 | for case in ['own_cmp']: |
---|
10 | print('%s\ncase= %s' % ('='*50, case)) |
---|
11 | |
---|
12 | if case in ['own_cmp']: |
---|
13 | def cmp(a,b): |
---|
14 | return (a > b) - (a < b) |
---|
15 | |
---|
16 | class TestClass(object): |
---|
17 | def __init__(self, val): |
---|
18 | self.val = val |
---|
19 | def __str__(self): |
---|
20 | return 'TestClass(val=%s)' % self.val |
---|
21 | |
---|
22 | if case in ['cmp', 'own_cmp']: |
---|
23 | def __cmp__(self, other): |
---|
24 | return cmp(self.val, other.val) |
---|
25 | |
---|
26 | |
---|
27 | for v1, v2 in ['aa', 'ab', 'ba']: |
---|
28 | t1 = TestClass(v1) |
---|
29 | t2 = TestClass(v2) |
---|
30 | print('t1= %s, id= %s' % (t1, id(t1))) |
---|
31 | print('t2= %s, id= %s\n' % (t2, id(t2))) |
---|
32 | |
---|
33 | for op in ['==', '!=', '< ', '<=', '> ', '>=']: |
---|
34 | try: |
---|
35 | cmp_t = eval('t1'+op+'t2') |
---|
36 | except TypeError: |
---|
37 | cmp_t = 'TypeError' |
---|
38 | cmp_v = eval('v1'+op+'v2') |
---|
39 | cmp_id = eval(str(id(t1))+op+str(id(t2))) |
---|
40 | ok = 'OK' if cmp_t==cmp_v else '' |
---|
41 | print('t1%st2: %-9s (val: %-5s; id: %-5s) %s' % |
---|
42 | (op, cmp_t, cmp_v, cmp_id, ok)) |
---|
43 | print('-'*50) |
---|