2

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

4

1 回答 1

0

你检查过你的缩进以确保它是一致的吗?这是一个经典的 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'

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

于 2012-11-30T23:49:38.187 回答