我在python oop中需要一些帮助,我想在python类中创建一个空列表作为实例,然后添加一个在该空列表中添加点的方法,并添加一个返回所有点坐标的字符串方法,如(x1,y1((x2,y2((x3,y3(。(xn,yn(这是我的代码,但它不起作用:
class Foo:
# This list is initially empty
def __init__(self, lst):
self.lst = []
# append_point method which takes a point as an argument and
# adds points to the instance variable.
def append_point(self, **point):
self.lst.append(point)
return tuple(self.lst)
# returns the coordinates of all points
def __str__(self):
return f'cordinates : {self.lst}'
if __name__ == '__main__':
cor = Foo([1,2,4,3,5,7])
print(cor)
class Points:
def __init__(self, points=None):
if points is None:
points = []
self.points = points
def add_point(self, point):
self.points.append(point)
def __str__(self):
return f'Points coordinates: {self.points}'
points = Points([(0,0),(0,1)])
points.add_point((1,1))
print(points)
输出:
Points coordinates: [(0, 0), (0, 1), (1, 1)]