2
我正在寫一些Python代碼的訪問,並有一個類如下Python中,我的對象聲稱沒有一種方法
class GO:
##irrelevant code
def getCenter(self):
xList = []
yList = []
# Put all the x and y coordinates from every GE
# into separate lists
for ge in self.GEList:
for point in ge.pointList:
xList.append(point[0])
yList.append(point[1])
# Return the point whose x and y values are halfway between
# the left- and right-most points, and the top- and
# bottom-most points.
centerX = min(xList) + (max(xList) - min(xList))/2
centerY = min(yList) + (max(yList) - min(yList))/2
return (centerX, centerY)
###more irrelevant code
def scale(self, factor):
matrix = [[factor,0,0],[0,factor,0],[0,0,1]]
for ge in self.GEList:
fpt = []
(Cx, Cy) = ge.getCenter()
for pt in ge.pointList:
newpt = [pt[0]-C[0],pt[1]-C[0],1]###OR USE TRANSLATE
spt = matrixPointMultiply(matrix, newpt)
finalpt = [spt[0]+C[0],spt[1]+C[0],1]
fpt.append(finalpt)
ge.pointList=fpt
return
每當我運行它,它說:AttributeError: circle instance has no attribute 'getCenter'
。 如何讓對象正確調用函數本身? 這是一個不好的問題,我正在學習,所以詳細的建議會有幫助。
您可以省略'self.GEList',但它似乎很重要。 (在任何情況下,根據錯誤消息,所討論的對象不是'GO'實例。) – delnan
你是怎麼調用這個代碼的? –
在另一個問題上,將GE分成x,y列表的更簡單方法是使用zip。例如,如果'GE = [(2,3),(4,5),(6,7)]',那麼在'[x,y] = zip(* GE)'後,我們有'x =(2 ,4,6)'和'y =(3,5,7)' –