TypeError: unsupported operand type(s) for + when using sum

Viewed 3206

I have a class with my own implementation of __add__:

class Point(namedtuple('Point', ['x', 'y', 'z'])):
    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y, self.z + other.z)

Addition works as expected:

l = [Point(0,0,0), Point(0,1,2)]
s = l[0]
for a in l[1:]:
    s = s + a

But when I use builtin sum I'm getting an error:

s = sum(l)

TypeError: unsupported operand type(s) for +: 'int' and 'Point'

What's wrong in my code? Doesn't sum use __add__? What more should I override?

2 Answers
Related