Python 3.x 使用sum函数在caretsian平面上添加点
代码给了我一个错误。它应该在笛卡尔平面上添加点Python 3.x 使用sum函数在caretsian平面上添加点,python-3.x,oop,sum,Python 3.x,Oop,Sum,代码给了我一个错误。它应该在笛卡尔平面上添加点 class Cluster(object): def __init__(self, x, y): self.center = Point(x, y) self.points = [] def update(self): sum_ = sum(self.points, Point(0, 0)) len_ = float(len(self.points))
class Cluster(object):
def __init__(self, x, y):
self.center = Point(x, y)
self.points = []
def update(self):
sum_ = sum(self.points, Point(0, 0))
len_ = float(len(self.points))
self.center = Point(sum_.x/len_, sum_.y/len_)
self.points = []
def add_point(self, point):
self.points.append(point)
sum_ = sum(self.points, Point(0, 0))
sum()
函数仅适用于支持加法的类型。类可以使用\uuuu add\uuu
函数实现添加。如果这确实是您面临的问题,请在点类a函数中定义如下:
类点:
# ...
定义添加(自身、其他):
返回点(self.x+other.x,self.y+other.y)