【发布时间】:2013-12-04 19:31:56
【问题描述】:
我写了这两种方法,我仍然看不出这两种方法之间有什么区别..到目前为止,我的类工作正常,但由于方法是相同的,我仍然不明白为什么当我这样做时:x+1它调用 add ,而 1+x 它调用 radd ?
def __add__(self,other):
assert isinstance(other,(Rational,int,str))
other=Rational(other)
n = self.n * other.d + self.d * other.n
d = self.d * other.d
return Rational(n, d)
def __radd__(self,other):
assert isinstance(other,(Rational,int,str))
other=Rational(other)
n =self.d * other.n + other.d * self.n
d=other.d * self.d
return Rational(n, d)
【问题讨论】:
标签: class oop python-3.x rational-number