You could write a Point
class and implement the arithmetic operators. A naïve example would be:
class Point(object):
def __init__(self, *args):
self.coords = args
def __add__(self, other):
return Point(*[(x + y) for (x, y) in zip(self.coords, other.coords)])
def __repr__(self):
coord = ', '.join("{}".format(x) for x in self.coords)
return "<Point ({})>".format(coord)
Which you could use like this:
ptA = Point(10.0, 20.0, 30)
ptB = Point(50, 50, 50)
>>> print ptA + ptB
<Point (60.0, 70.0, 80)>
You could expand it to use __sub__
and __mul__
, and make it a bit more robust, because right now the implementation assumes many things:
- That you always add points (this would break if you add an integer, for example)
- That both points are the same dimension
All those things could be tested, to then generalize the methods.