|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | from math import pi |
| 8 | +import functools |
8 | 9 |
|
9 | 10 |
|
| 11 | +@functools.total_ordering |
10 | 12 | class Circle(object): |
11 | 13 |
|
12 | 14 | def __init__(self, radius): |
@@ -56,8 +58,33 @@ def __imul__(self, factor): |
56 | 58 | def __rmul__(self, factor): |
57 | 59 | return Circle(self.radius * factor) |
58 | 60 |
|
59 | | - def __cmp__(self, other): |
60 | | - return cmp(self.radius, other.radius) |
| 61 | + # def __cmp__(self, other): |
| 62 | + # """ |
| 63 | + # This is the easy way to support comparing all in one shot |
| 64 | + # """ |
| 65 | + # return cmp(self.radius, other.radius) |
| 66 | + |
| 67 | + ## Or you can define them all |
| 68 | + ## So can support odd situations |
| 69 | + # def __eq__(self, other): |
| 70 | + # return self.radius == other.radius |
| 71 | + # def __ne__(self, other): |
| 72 | + # return self.radius != other.radius |
| 73 | + # def __gt__(self, other): |
| 74 | + # return self.radius > other.radius |
| 75 | + # def __ge__(self, other): |
| 76 | + # return self.radius >= other.radius |
| 77 | + # def __lt__(self, other): |
| 78 | + # return self.radius < other.radius |
| 79 | + # def __le__(self, other): |
| 80 | + # return self.radius <= other.radius |
| 81 | + |
| 82 | + ## or you can put the @total_ordering decorator on the class definiton and do this: |
| 83 | + def __eq__(self, other): |
| 84 | + return self.radius == other.radius |
| 85 | + def __gt__(self, other): |
| 86 | + return self.radius > other.radius |
| 87 | + |
61 | 88 |
|
62 | 89 |
|
63 | 90 | class SubCircle(Circle): |
|
0 commit comments