Python TypeError:/:';理性';和';理性';

Python TypeError:/:';理性';和';理性';,python,Python,当我尝试实现Rational(4,1)/Rational(2,1) 但它显示了/:'Rational'和'Rational'的类型错误“不支持的操作数类型”,这表明未实现除法函数。 我认为除法函数应该像上面的代码“div”那样实现,所以我现在不知道这段代码有什么问题。谁能给点建议吗 Python3.x中没有使用\uuuu div\uuuu()方法。改为实施\uuu truediv\uuu() def gcd(bigger, smaller): """Calculate the great

当我尝试实现Rational(4,1)/Rational(2,1) 但它显示了/:'Rational'和'Rational'的
类型错误“不支持的操作数类型”,这表明未实现除法函数。
我认为除法函数应该像上面的代码“div”那样实现,所以我现在不知道这段代码有什么问题。谁能给点建议吗

Python3.x中没有使用
\uuuu div\uuuu()
方法。改为实施
\uuu truediv\uuu()

def gcd(bigger, smaller):
    """Calculate the greatest common divisor of two positive integers."""
    print('in gcd')
    if not bigger > smaller:        # swap if necessary so bigger > smaller
        bigger, smaller = smaller, bigger
    while smaller != 0:                         # 1. if smaller == 0, halt
        remainder = bigger % smaller            # 2. find remainder
        # print('calculation, big:{}, small:{}, rem:{}'.\
        #      format(bigger, smaller, remainder))  # debugging
        bigger, smaller = smaller, remainder    # 3. reapply
    return bigger

def lcm (a,b):
    """Calculate the lowest common multiple of two positive integers."""
    print('in lcm')
    return (a*b)//gcd(a,b)  # Equation 12.1, // ensures int is returned

class Rational(object):
    """ Rational with numerator and denominator. Denominator
    parameter defaults to 1"""
    def __init__(self,numer,denom=1):
        print('in constructor')
        self.numer = numer
        self.denom = denom

    def __str__(self):
        """ String representation for printing"""
        print('in str')
        return str(self.numer)+'/'+str(self.denom)

    def __repr__(self):
        """ Used in interpreter. Call __str__ for now """
        print('in repr')
        return self.__str__()

    def __div__(self, param_Rational):
        print("in div")
        new_param = Rational(param_Rational.denom, param_Rational.numer)
        return self.__mul__(new_param)


    def __mul__(self, param_Rational):
        print("in mult")
        if type(param_Rational) == int:
            param_Rational = Rational(param_Rational)
        if type(param_Rational) == Rational:
            new_numerator = self.numer * param_Rational.numer
            new_denominator = self.denom * param_Rational.denom
            return Rational(new_numerator, new_denominator)
        else:
            raise("type error")

    def __rmul__(self, param):
        print("in rmul")
        return self.__mul__(param)