+ -
当前位置:首页 → 问答吧 → python中如何暂时重写元组的+运算符

python中如何暂时重写元组的+运算符

时间:2011-04-24

来源:互联网

本帖最后由 thanyouqdx 于 2011-04-24 20:23 编辑

使 (1,1,1) + (1,1,1)   等于 (2, 2, 2)

我的代码
  1. def __add__(self, other):
  2.     return (self[0] + other[1],self[0] + other[1],self[2] + other[2])

  3. if __name__ == '__main__':
  4.     print( (1,1,1)+(1,1,1) )
  5.     print(__add__((1,1,1),(1,1,1)))
复制代码
结果为
(1, 1, 1, 1, 1, 1)
(2, 2, 2)

__add__ 倒是被重写了
+ 确没有

作者: thanyouqdx   发布时间: 2011-04-24

把加写到你自己的类里边

作者: yjphhw   发布时间: 2011-04-24