__lt__
,__gr__
,__ge__
__le__
。
我的一个方法:
def __lt__(self,other):
n1=self.n
d1=self.d
if isinstance(other,Rational):
n2=other.n
d2=other.d
elif isinstance(other,int):
n2=other
d2=1
return (n1/d1)<(n2/d2)
答案 0 :(得分:0)
例如:
>>> r2=Rational("0.25")
>>> r2
<Rational 1/4>
>>> r2<4
True
>>> 4<r2
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
4<r2
TypeError: unorderable types: int() < Rational()
>>>