4

Hello I'm having an issue calling a function on an object in Python. This is the Method im trying to call

def getCenter(self):
    cx = 0
    cy = 0
    for p in self.points:
        cx += p.x
        cy += p.y
    cx /= len(self.points)
    cy /= len(self.points)
    return Point(cx,cy,self.cid)

this is the call im trying to make

for c in clusters:
    print(c.points,c.cid)
    poi = c.getCenter
    print(poi.x)

clusters have a list of points called "points". A point looks like this

class Point:
    x = 0
    y = 0
    cluster = -1
    def __init__(self, x = 0, y = 0):
        self.x = x
        self.y = y

I'm getting the error message 'function' object has no attribute 'x'. I'm calling the getCenter-method on a Cluster which returns a point. And im calling the x attribut on that point. So i dont know why this error is appearing

1 Answer 1

4

This line here:

poi = c.getCenter

Is not calling the function, it's assigning the function to the name poi. You need parenthesis to actually call it:

poi = c.getCenter()
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers mate, helped me a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.