为什么当我重写 class 中的 __div__ 方法时,它在我测试代码时不起作用?

How come when I overide the __div__ method in a class, it dowsn't work when I am testing the code?

我想写一个允许算术运算符的分数class,但是当我定义div方法时它似乎不能不过 divide。

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

您需要在 Python 中使用 __truediv__ 3