为分数覆盖 python 中的 __iadd__
Overriding __iadd__ in python for fractions
我正在尝试用分数重写 python 中的 __iadd__
方法,现在这就是我所做的。请有人检查一下我是否做对了。我有 this and ,但这不是我想要的。应该从class
的角度来使用。
我的__iadd__
代码:
def __iadd__(self, other):
"""
Implementation of the '+='
augmented function
:param other:
:return:
"""
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
v = Fraction(newnum, newden)
return v
这是在 class Fraction
结构中完成的:
def gcd(m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
class Fraction:
# initializing variables for class
def __init__(self, top, bottom):
# check if entered fraction is an integer
if isinstance(top, int) and isinstance(bottom, int):
# reduce the given fractions to lowest term
common = gcd(top, bottom)
self.num = abs(top) // common
self.den = abs(bottom) // common
else:
raise "Please only integers are allowed"
def __str__(self):
return str(self.num) + "/" + str(self.den)
这实际上是 return 像这样完成时的写入值:
f1 = Fraction(1, 2)
f2 = Fraction(8, 10)
f1 += f2
print(f1)
也通过调用重写的 __add__
方法做到了:
def __iadd__(self, other):
"""
Implementation of the '+='
augmented function
:param other:
:return:
"""
if other == 0:
return self
else:
return self.__add__(other)
覆盖__add__
:
def __add__(self, otherfraction):
newnum = self.num * otherfraction.den + self.den * otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
- 使用
__iadd__
就地递增。
- 使用
__add__
增加并创建一个新实例。
因此,您可以按如下方式更改代码。
def __iadd__(self, other):
self.num = self.num * other.den + self.den * other.num
self.den = self.den * other.den
return self
另见这个问题:implementing add and iadd for custom class in python?
请注意 Python 有一个 Rational numbers 模块。检查源代码...但是 Fraction
对象是不可变的,所以 __iadd__
没有实现。
我正在尝试用分数重写 python 中的 __iadd__
方法,现在这就是我所做的。请有人检查一下我是否做对了。我有 this and class
的角度来使用。
我的__iadd__
代码:
def __iadd__(self, other):
"""
Implementation of the '+='
augmented function
:param other:
:return:
"""
newnum = self.num * other.den + self.den * other.num
newden = self.den * other.den
v = Fraction(newnum, newden)
return v
这是在 class Fraction
结构中完成的:
def gcd(m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
class Fraction:
# initializing variables for class
def __init__(self, top, bottom):
# check if entered fraction is an integer
if isinstance(top, int) and isinstance(bottom, int):
# reduce the given fractions to lowest term
common = gcd(top, bottom)
self.num = abs(top) // common
self.den = abs(bottom) // common
else:
raise "Please only integers are allowed"
def __str__(self):
return str(self.num) + "/" + str(self.den)
这实际上是 return 像这样完成时的写入值:
f1 = Fraction(1, 2)
f2 = Fraction(8, 10)
f1 += f2
print(f1)
也通过调用重写的 __add__
方法做到了:
def __iadd__(self, other):
"""
Implementation of the '+='
augmented function
:param other:
:return:
"""
if other == 0:
return self
else:
return self.__add__(other)
覆盖__add__
:
def __add__(self, otherfraction):
newnum = self.num * otherfraction.den + self.den * otherfraction.num
newden = self.den * otherfraction.den
return Fraction(newnum, newden)
- 使用
__iadd__
就地递增。 - 使用
__add__
增加并创建一个新实例。
因此,您可以按如下方式更改代码。
def __iadd__(self, other):
self.num = self.num * other.den + self.den * other.num
self.den = self.den * other.den
return self
另见这个问题:implementing add and iadd for custom class in python?
请注意 Python 有一个 Rational numbers 模块。检查源代码...但是 Fraction
对象是不可变的,所以 __iadd__
没有实现。