Python程序不能调用“float”对象

2024-04-19 09:48:15 发布

您现在位置:Python中文网/ 问答频道 /正文

这个python程序设计用于查找圆的面积和周长,但是由于某种原因,当我试图执行area方法时,我得到了一个TypeError: 'float' object is not callable错误

class Circle:
    pi = 3.14

    def __init__(self, radius=1):
        self.radius = radius
        self.area = radius * radius * self.pi

    def circum(self):
        return self.pi * self.radius * 2


my_circle = Circle(30)
my_circle.area()

错误

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    my_circle.area()
TypeError: 'float' object is not callable

Tags: selfobjectismydef错误pinot
3条回答

Circle.area不是一个方法。这是一个简单的变量。你可以用my_circle.area读它

如果你想让它成为一个函数,那么你可以改变半径并重新计算,那么你需要让它成为一个函数

    def area(self):
        return self.radius * self.radius * self.pi

说明:

问题出在哪里很简单self.area是在对象中存储和初始化的变量,而不是函数。因此,您可以直接访问它,而无需在函数调用的末尾使用()(或初始化类的对象,请参阅附加说明)。检查下面的代码和注释

Additional Note: You will see the use of Circle(30) to initialize an object. This convention is used to pass parameters to the __init__ function inside the class while instantiating an object, and it looks the same as if calling a function!

class Circle:
    pi = 3.14

    def __init__(self, radius=1):
        self.radius = radius
        self.area = radius * radius * self.pi

    def circum(self):
        return self.pi * self.radius * 2


my_circle = Circle(30)

#View all the variables initialized in your object
my_circle.__dict__
>>{'radius': 30, 'area': 2826.0}

#Fetch a variable
my_circle.area
>>2826.0

#Call a function
my_circle.circum()
>>188.4

修改:

根据您在描述中提到的内容,这是您应该如何理想地定义您提到的类-

class Circle:
    def __init__(self, radius=1):
        self.pi = 3.14
        self.radius = radius

    def circum(self):
        self.circum = self.pi * self.radius * 2
        print(self.circum)

    def area(self):
        self.area = self.radius * self.radius * self.pi
        print(self.area) 
        
circle = Circle(30)

#Only radius defined till now
circle.__dict__
#>>{'pi': 3.14, 'radius': 30}


circle.circum()
#>>188.4

circle.area()
#>>2826.0


#After calling the 2 functions, the self dict gets updated with area and circumference
circle.__dict__
#>>{'pi': 3.14, 'radius': 30, 'circum': 188.4, 'area': 2826.0}

思维过程

The way I like to imagine objects of a class is that it is an object is a database with some optional explicit functions defined. The __init__ function initialized the database. You can access everything that the database contains by using the self. Therefore when you pass self to a function, you giving that function access to all of the database that the object is storing. To print everything in that database self you can use my_circle.__self__

我想解释一下错误的含义。 在python中,有可调用和非可调用。不仅可以调用函数,还可以调用类的实例

float对象不可调用(self.area为float)。如果类定义了__call__方法,则该类是可调用的

1.1()
TypeError: 'float' object is not callable

如果调用1.1(),将得到相同的错误。因为1.1是float类型,并且float没有__call__方法

看看下面的例子

class callable_area(float):
    def __call__(self, x):
        print('My parent is float and can not be called, but i can be')
        print(self * x)
# Product of two numbers
callable_area(1.1)(2.2)

callable_area(1.1)不是函数,而是类的实例,但我仍然可以这样调用它callable_area(1.1)(2.2)

现在可能你得到的错误只是一个无意识的错误,你把它转换成函数,或者去掉它前面的空括号,然后去掉错误,但是知道错误意味着什么也是个好主意

相关问题 更多 >