gpt4 book ai didi

Python,我的对象声称无法访问方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:09 24 4
gpt4 key购买 nike

我正在编写一些 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'。如何让对象正确调用自身的函数?这是一个菜鸟问题,我正在学习,所以详细的建议会很有帮助。

最佳答案

您是否检查过缩进以确保它们完全一致?这是一个经典的 Python 初学者问题。您需要使用一致的空格(制表符或空格,大多数人更喜欢空格)和适量的空格。

例如,这可能看起来不错,但它不会达到您的预期:

class Dummy(object):

def foo(self):
print "foo!"

def bar(self):
print "bar!"

d = Dummy()
d.bar()

这将返回:

AttributeError: 'Dummy' object has no attribute 'bar'

如果不是这样,请尝试将您的代码减少到最低限度,然后发布它以及您如何调用它。就目前而言,一般形式对我来说看起来没问题,除非我遗漏了什么。

关于Python,我的对象声称无法访问方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13654742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com