我有期望,reduce(add,lst)
和sum(lst)
应该给我相同的结果名单上,但`减少(增加,...)``VS总和(...)`,为什么第二次失败的对象
In [18]: class p():
def __init__(self, x, y):
self.x=x ; self.y=y
def __repr__(self):
return "(%r,%r)"%(self.x,self.y)
def __add__(self, P):
return p(self.x+P.x, self.y+P.y)
....:
In [19]: pts=[p(1,0), p(2,1), p(-3,4)]
In [20]: from operator import add
In [21]: print reduce(add,pts)
(0,5)
In [22]: print sum(pts)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-98a81789d257> in <module>()
----> 1 print sum(pts)
TypeError: unsupported operand type(s) for +: 'int' and 'instance'
In [23]:
当然我不理解的东西,可能很明显;有人能教导我吗?