Methods in Class Rational

117 views Asked by At

im almost done with my Class Rational, having a little problem when implementing comparison. when i compare a Rational number with int, if the Rational is the left operand, then all is well, but when the comparison is int < Rational, it doesn't work.. having this problem with methods like : __lt__ , __gr__ , __ge__ __le__. one of my methods:

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)
1

There are 1 answers

0
user3025851 On

for example:

>>> 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()
>>>