Python 为什么当我在一个类中覆盖了uu div_uu_u_u_u_u_u_u_u_u_u_u_u_u_u_uu_uu_u)方法时,它是';我在测试代码时不工作?

Python 为什么当我在一个类中覆盖了uu div_uu_u_u_u_u_u_u_u_u_u_u_u_u_u_uu_uu_u)方法时,它是';我在测试代码时不工作?,python,class,math,operators,Python,Class,Math,Operators,我想编写一个允许算术运算符的分数类,但是当我定义div方法时,它似乎无法除法 from math import gcd class Fraction: def __init__(self, n, d): self.n = n self.d = d def __add__(self, other): newn = self.n * other.d + self.d * other.n newd = self.d *

我想编写一个允许算术运算符的分数类,但是当我定义div方法时,它似乎无法除法

from math import gcd

class Fraction:
    def __init__(self, n, d):
        self.n = n
        self.d = d
    def __add__(self, other):
        newn = self.n * other.d + self.d * other.n
        newd = self.d * other.d
        common = gcd(newn, newd)
        return Fraction(newn/common, newd/common)
    def __sub__(self, other):
        newn = self.n * other.d - self.d * other.n
        newd = self.d * other.d
        common = gcd(newn, newd)
        return Fraction(newn/common, newd/common)
    def __div__(self, other):
        newn = self.n * other.d
        newd = self.d * other.n
        common = gcd(newn, newd)
        return Fraction(newn/common, newd/common)
    def __mul__(self, other):
        newn = self.n * other.n
        newd = self.d * other.d
        common = gcd(newn, newd)
        return Fraction(newn/common, newd/common)
    def __repr__(self):
        return "{}/{}".format(int(self.n), int(self.d))

print(Fraction(1, 2) / Fraction(1, 4))

您需要在Python3中使用
\uuuuu-truediv\uuuu
您正在使用Python3,Python3分别使用
\uu-truediv\uuuu
\uu-floordiv\uuu
来表示
//code>。谢谢你的帮助,谢谢!成功了!