【发布时间】:2011-01-17 01:41:24
【问题描述】:
我是 Python 新手,如果这是一个愚蠢的问题,请提前道歉。
对于一个作业,我需要为 myInt 类重载增强算术作业(+=、-=、/=、*=、**=、%=)。我检查了 Python 文档,这就是我想出的:
def __iadd__(self, other):
if isinstance(other, myInt):
self.a += other.a
elif type(other) == int:
self.a += other
else:
raise Exception("invalid argument")
self.a 和 other.a 引用存储在每个类实例中的 int。我尝试按如下方式对此进行测试,但每次我得到“无”而不是预期值 5:
c = myInt(2)
b = myInt(3)
c += b
print c
谁能告诉我为什么会这样?提前致谢。
【问题讨论】:
-
我相信没有愚蠢的问题..
标签: python operators operator-overloading