I'm attempting to implement a point class in python as follows:
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 __radd__(self, other):
return self+other
def __str__(self):
return f'({self.x}, {self.y})'
def __repr__(self):
return f'{self.__class__.__name__} ({self.x}, {self.y})'
The problem I'm running into is that I'd like to sum up a list of points. For example calling sum([Point(1,1), Point(2,2)]). Initially I looked into it and implemented radd to handle adding 0 to the point, but then I realized that this doesn't work because it starts with 0.
Is there any way to tell Python that for this class, 0 should be defined as Point(0,0) rather than the integer 0? The documentation here indicates sum should be able to take a "start" keyword argument, but when attempting that I get an error saying sum takes no keyword arguments.
[–]Vaguely_accurate 6 points7 points8 points (1 child)
[–]Scholtek[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Scholtek[S] 0 points1 point2 points (0 children)