首页 > 解决方案 > 如何在同一个类方法中调用具有多个参数的函数?

问题描述

下面是我的 Fraction 类的代码,我想在我的简化类中调用 add 类。

Class Fraction:
      
    def __add__(self, other: "Fraction") -> "Fraction": 
 
        n: int = (self.num * other.denom + self.denom * other.num) / self.simplify()
        d: int = (self.denom * other.denom) / self.simplify()
           
    def simplify(self) -> "Fraction":

    """ Function to find the gcd of the fraction and return the simplified fraction"""
        **a = self.__add__()**
        n1: Fraction = abs(self.num)
        n2: Fraction= abs(self.denom)
        gcd: int = 1

        k:int = 1
        while k <= n1 and k <= n2:
            if n1 % k == 0 and n2 % k == 0:
                gcd = k
            k += 1
        return gcd

粗体代码是错误的,当我运行上面的代码时,它会抛出“No value for argument “other”,我该如何调用这个方法?

标签: pythonpython-3.xclassselffractions

解决方案


推荐阅读