Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: 'function' object has no attribute x

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

like image 776
Hubter Avatar asked Nov 15 '25 18:11

Hubter


1 Answers

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()
like image 82
Markus Meskanen Avatar answered Nov 17 '25 07:11

Markus Meskanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!