__rsub__ 和 __rtruediv__ 带分数

发布于 2024-12-13 20:58:46 字数 1184 浏览 0 评论 0原文

我正在尝试在我创建的名为 Fraction 的类中使用 __rsub__ 函数。

这是 Fraction 类代码:

def __init__(self, num, denom):
    ''' Creates a new Fraction object num/denom'''
    self.num = num
    self.denom = denom
    self.reduce()

def __repr__(self):
    ''' returns string representation of our fraction'''
    return str(self.num) + "/" + str(self.denom)

def reduce(self):
    ''' converts our fractional representation into reduced form'''
    divisor = gcd(self.num, self.denom)
    self.num = self.num // divisor
    self.denom = self.denom // divisor
def __sub__(self, other):
    if isinstance(other,Fraction) == True:
        newnum = self.num * other.denom - self.denom*other.num
        newdenom = self.denom * other.denom
        return Fraction(newnum, newdenom)

现在,如果我使用以下方法执行 __radd____rmul__return self + otherreturn self * other< /code> 分别,它将执行所需的结果。但是,仅通过更改运算符来执行 __rsub____rtruediv__ 是行不通的。我该如何解决这个问题?

本质上,调用函数的代码是:

f = Fraction(2,3)
g = Fraction(4,8)
print("2 - f: ", 2 - f)
print("2 / f: ", 2 / f)

感谢您的帮助!

I'm attempting to use the __rsub__ function in a class I've made called Fraction.

Here's the Fraction class code:

def __init__(self, num, denom):
    ''' Creates a new Fraction object num/denom'''
    self.num = num
    self.denom = denom
    self.reduce()

def __repr__(self):
    ''' returns string representation of our fraction'''
    return str(self.num) + "/" + str(self.denom)

def reduce(self):
    ''' converts our fractional representation into reduced form'''
    divisor = gcd(self.num, self.denom)
    self.num = self.num // divisor
    self.denom = self.denom // divisor
def __sub__(self, other):
    if isinstance(other,Fraction) == True:
        newnum = self.num * other.denom - self.denom*other.num
        newdenom = self.denom * other.denom
        return Fraction(newnum, newdenom)

Now if I do __radd__ or __rmul__ by using: return self + other or return self * other respectively, it will perform the desired result. However, doing __rsub__ and __rtruediv__ do not work by simply changing the operator. How can I fix this?

Essentially, the code calling the functions is:

f = Fraction(2,3)
g = Fraction(4,8)
print("2 - f: ", 2 - f)
print("2 / f: ", 2 / f)

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残月升风 2024-12-20 20:58:46

您首先需要将 other 转换为 Fraction 才能完成此操作:

def __rsub__(self, other):
    return Fraction(other, 1) - self

因为 __rsub__() 仅在 other 时才会被调用> 不是Fraction 类型,我们不需要任何类型检查——我们只是假设它是一个整数。

您当前的 __sub__() 实现还需要一些工作 - 如果 other 不具有 Fraction 类型,则它不会返回任何内容。

You first need to convert other to a Fraction to make this work:

def __rsub__(self, other):
    return Fraction(other, 1) - self

Since __rsub__() only gets called if other is not of type Fraction, we don't need any type checking -- we simply assume it is an integer.

Your current implementation of __sub__() also needs some work -- it returns nothing if other does not have the type Fraction.

临风闻羌笛 2024-12-20 20:58:46

因为您进行类型检查,并在第二个操作数不是 Fraction 时返回 None (另外,if isinstance(...):,而不是if isinstance(...) == True:)。相反,你需要强制论证。

Because you type-check, and return None when the second operand is not Fraction (also, if isinstance(...):, not if isinstance(...) == True:). You need to coerce the argument instead.

夏尔 2024-12-20 20:58:46

实现“r”操作的通常方法是 1) 检查以确保 other 是您知道如何处理的类型 2) 如果不是,则返回 NotImplemented,3) 如果是,则转换为可以与自身交互的类型:

def __radd__(self, other):
    if not instance(other, (int, Fraction):
        return NotImplemented
    converted = Fraction(other)
    return converted + self

The usual way to implement an "r" operation is 1) check to make sure other is a type you know how to handle 2) if not, return NotImplemented, and 3) if so, convert is to a type than can interact with self:

def __radd__(self, other):
    if not instance(other, (int, Fraction):
        return NotImplemented
    converted = Fraction(other)
    return converted + self
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文