【发布时间】:2015-05-23 05:32:16
【问题描述】:
我正在尝试在 python 中重载除法运算符。
class Fraction:
def __init__(self,top,bottom):
def gcd(m, n):
while m % n != 0:
old_m = m
old_n = n
m = old_n
n = old_m % old_n
return n
common = gcd(top,bottom)
self.num = top/common
self.den = bottom/common
def __str__ (self):
return str(self.num) + "/" + str(self.den)
def get_num(self):
return self.num
def get_den(self):
return self.den
def __add__(self, other_fraction):
new_num = self.num * other_fraction.den + self.den * other_fraction.num
new_den = self.den * other_fraction.den
return Fraction(new_num, new_den)
def __sub__(self, other_fraction):
new_num = self.num * other_fraction.den - self.den * other_fraction.num
new_den = self.den * other_fraction.den
return Fraction(new_num, new_den)
def __mul__ (self, other_fraction):
new_num = self.num * other_fraction.num
new_den = self.den * other_fraction.den
return Fraction(new_num, new_den)
def __truediv__(self, other_fraction):
new_num = self.num * other_fraction.den
new_den = self.den * other_fraction.num
return Fraction(new_num, new_den)
def __eq__(self, other):
first_num = self.num * other.den
second_num = other.num * self.den
return first_num == second_num
a = Fraction(10,20)
b = Fraction(30,20)
print a
print "numerator is",a.get_num()
print "denominator is",a.get_den()
print "equality is",(a==b)
print "sum is",(a+b)
print "difference is",(a-b)
print "product is",(a*b)
print "division is",(a/b)
但我在__truediv__ 收到错误:
TypeError: /: 'instance' 和不支持的操作数类型 '实例'
代码有什么问题?
【问题讨论】:
-
from fraction import Fraction
标签: python python-2.7 overloading operator-keyword