0

This python program was designed to find the area and circumference of a circle, but for some reason, I'm getting a TypeError: 'float' object is not callable error when I'm trying to execute the area method.

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()

Error

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    my_circle.area()
TypeError: 'float' object is not callable
6
  • Post the full traceback message instead of just a summary of the error. Commented Aug 10, 2021 at 4:20
  • line 309, in <module> my_circle.area() TypeError: 'float' object is not callable Commented Aug 10, 2021 at 4:23
  • My_circle.area is a non callable class objects....so don't call it using (). Btw there are also callable type of objects in python. Look up on that Commented Aug 10, 2021 at 4:23
  • It should be in the question itself and not line 309. You posted a running example (that's good!) and the question should be written in terms of it. Commented Aug 10, 2021 at 4:24
  • @SouvikDatta, i have also added a slight variation on how you should be ideally writing the class based on what you describe in the description. Do check that out. Commented Aug 10, 2021 at 4:41

3 Answers 3

4

Circle.area is not a method. It is a simple variable. You can read it with my_circle.area.

If you want it to be a function, so you can change the radius and recompute, then you need to make it a function.

    def area(self):
        return self.radius * self.radius * self.pi
Sign up to request clarification or add additional context in comments.

Comments

2

Explanation:

It's quite straightforward where the issue is. self.area is a variable that is being stored and initialized in the object, not a function. Therefore you can just access it without using () at the end which is used for a function call (or initializing objects of a class, see additional note). Check the code and comments below.

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

Modification:

Based on what you mention in description this is how you should ideally defined the class you are mentioning -

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}

Thought process

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__

3 Comments

There are also callable type of objects, clarify on that
adding that info
I meant this tutorialsteacher.com/python/callable-method Where instancea ofclass are callable. Torch library is full of those. There is reason why it fixes on object is not callable type
0

I want to explain meaning of error. In python there are callable and non callables. Not only functions but also instances of classes can be callable.

float object is not callable (self.area is float). A class is callable if it has __call__ method defined.

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

You will get the same error if you call 1.1() . Because 1.1 is of type float and float does not have __call__ method.

Look at example below

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) is not a function and is instance of class, but still i can call it like this callable_area(1.1)(2.2).

Now maybe the error you get is just a unconscious mistake and you convert it into function or remove empty brackets infront of it and remove error, but it is also good idea to know what error is implying

Comments

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.