简体   繁体   English

“浮动”对象不可调用错误

[英]'float' object is not callable error

I have a function in a class I have written that reads a reading on a magnetometer and converts it into a direction between 0 and 359 degrees.我在我编写的课程中有一个函数,它读取磁力计上的读数并将其转换为 0 到 359 度之间的方向。 The function is as follows:功能如下:

def heading(self):
    self.xzy = self.__GetCompassMag()
    self.x = self.xzy[0]
    self.y = self.xzy[2]
    pi = 3.14159
    self.heading = round((math.atan2(self.y, self.x) * 180) / pi)

    if self.heading < 0:
        return int(360 + self.heading)
    else:
        return int(self.heading)

When I try to call the function with print obj.heading() , the python shell gives my the error "TypeError: 'float' object is not callable".当我尝试使用print obj.heading()调用该函数时,python shell 给出了我的错误“TypeError: 'float' object is not callable”。 I have some experience in coding, but not much in python.我在编码方面有一些经验,但在 python 方面经验不多。 Does anyone know what is going on here?有谁知道这里发生了什么?

Because you override you heading method in a plain instance variable in this line因为您在此行中的普通实例变量中覆盖了heading方法

self.heading = round((math.atan2(self.y, self.x) * 180) / pi

Don't use self to write your local variable because with self you will be referring an instance variable, try todo this:不要使用self来编写您的局部变量,因为使用self您将引用一个实例变量,请尝试这样做:

def heading(self):
    self.xzy = self.__GetCompassMag()
    self.x = self.xzy[0]
    self.y = self.xzy[2]
    pi = 3.14159
    heading = round((math.atan2(self.y, self.x) * 180) / pi)

    if heading < 0:
        return int(360 + heading)
    else:
        return int(heading)
print obj.heading()  

needs to be:需要是:

print obj.heading

take off the bracket.取下支架。 anyways i saw this because i encountered the same problem while doing my own project using an object.无论如何,我看到了这一点,因为我在使用对象做我自己的项目时遇到了同样的问题。 (course im using python 3) (当然我使用python 3)

hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM