__rsub__ 和 __rtruediv__ 带分数
我正在尝试在我创建的名为 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 + other
或 return 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您首先需要将
other
转换为Fraction
才能完成此操作:因为
__rsub__()
仅在other
时才会被调用> 不是Fraction
类型,我们不需要任何类型检查——我们只是假设它是一个整数。您当前的
__sub__()
实现还需要一些工作 - 如果other
不具有Fraction
类型,则它不会返回任何内容。You first need to convert
other
to aFraction
to make this work:Since
__rsub__()
only gets called ifother
is not of typeFraction
, 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 ifother
does not have the typeFraction
.因为您进行类型检查,并在第二个操作数不是
Fraction
时返回None
(另外,if isinstance(...):
,而不是if isinstance(...) == True:
)。相反,你需要强制论证。Because you type-check, and return
None
when the second operand is notFraction
(also,if isinstance(...):
, notif isinstance(...) == True:
). You need to coerce the argument instead.实现“r”操作的通常方法是 1) 检查以确保 other 是您知道如何处理的类型 2) 如果不是,则返回 NotImplemented,3) 如果是,则转换为可以与自身交互的类型:
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: