【发布时间】:2016-03-12 08:59:40
【问题描述】:
我已经编写了这段 sn-p 代码来说明问题。在代码本身下方,您可以看到控制台打印输出。
在我的程序中,Polygon 类对象将顶点坐标存储在指向每个顶点的向量列表中。 Translate() 函数所要做的就是遍历列表中的每个向量并将参数向量添加到每个项目。很简单吧?
Vector 类有自己的重载__add__ 函数。
当我编写和测试代码时,我发现该列表的成员只有在我迭代时才会发生变化。完成后,所有坐标都会恢复为原始值。
发现这个问题后,凭直觉我又做了一个函数——Manual_Translate(),手动计算向量分量(不调用Vector.__add__)
class Vector():
def __init__(self, X, Y):
self.x = X
self.y = Y
def __add__(self, Other_Vector):
return Vector((self.x + Other_Vector.x),(self.y + Other_Vector.y))
class Polygon():
def __init__(self, point_list):
self.Points = []
for item in point_list:
self.Points.append (Vector(item[0], item[1]))
def Translate (self, Translation):
for point in self.Points:
point += Translation
print (point.x, point.y) #printout from the same loop
def Manual_Translate (self, Translation):
for point in self.Points:
point.x += Translation.x
point.y += Translation.y
print (point.x, point.y)
def Report (self):
for point in self.Points:
print (point.x, point.y) #printout from another loop
vertices = [[0,0],[0,100],[100,100],[100,0]]
Square = Polygon (vertices)
print ("A: we used __add__ function")
Square.Translate (Vector(115,139))
print ("")
Square.Report()
print ("\nB: we calculated vector sum by hand")
Square.Manual_Translate (Vector(115,139))
print ("")
Square.Report()
结果:
如果我使用__add__,值更改会丢失。如果我手动添加向量 - 他们会留下来。我错过了什么? __add__ 实际上与这个问题有什么关系吗?怎么回事?
A: we used __add__ function
115 139
115 239
215 239
215 139
0 0
0 100
100 100
100 0
B: we calculated vector sum by hand
115 139
115 239
215 239
215 139
115 139
115 239
215 239
215 139
【问题讨论】:
-
@R Nar 谢谢,已修复。
-
赞成。见this SO QA
标签: python iteration overloading