As I understand it, for it to be possible to cast sum() on an object, it must be an iterable, and must be "addable", i.e. it must implement methods __iter__ and __add__. However, when I do this for my class Point (just an example), this doesn't work.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __iter__(self):
return self
def __str__(self):
return str((self.x, self.y))
print(Point(2, 2) + Point(1, 1))
>>> (3, 3) # As expected; good!
points = [Point(0, 0), Point(2, 0), Point(0, 2), Point(2, 2)]
print(sum(points)) # Expect (4, 4)
>>> TypeError: unsupported operand type(s) for +: 'int' and 'Point'
If I implement __radd__ the same as __add__ I then get an attribute error when I try sum():
AttributeError: 'int' object has no attribute 'x'
Based on the errors my Points are somewhere being separated to just ints but I'm not sure where.
Thanks for any help.
sumis not required to work for non-numeric types. It's not entirely clear, though, whatsumwill consider a numeric type. (For example, it works forlists, but explicitly rejectsstrs.)''.join()is a better alternative for string concatenation thansum()? Edit, see this answersumcan't be implemented to calljoinitself, rather than raise an exception.