Python TypeError: ‘int’ object is not callable Solution

Posted in /  

Python TypeError: ‘int’ object is not callable Solution
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    In Python, we can use the parenthesis " () " for multiple purposes such as we can use them as small brackets to compute mathematical computation e.g (a+b)*c , define a tuple e.g a=(1,2,3) or use them in the function definition and calling e.g function_name() .

    But if we mistreat the mathematical computation parenthesis usages with function calling parenthesis, the Python interpreter throws the TypeError: ‘int’ object is not callable error.

    In this Python error guide, we will walk through this Python error and discuss why it raises and how to solve it. We will also discuss some examples that will give you a better understanding of this Python error. So let's get started.

    The Python Error: "TypeError: ‘int’ object is not callable"

    The Error statement is itself divided into two parts

    1. Error Type TypeError : The TypeError occurs in the Python program when we mishandle the property of one data type with another.
    2. Error Message int object is not callable : This is the error message, that is telling us we are trying to call a function using an integer variable name.

    Why this Error Occur in Python

    According to the Python syntax, to call a function, we need to write the function name followed by the parenthesis.

    Example

    def my_function():
        print("This is a function")
    
    # call function
    my_function()

    But if, instead of the function name, we put the parenthesis after an integer value or variable, we receive the "TypeError: ‘int’ object is not callable" error.

    Example

    def my_function():
        print("This is a function")
    
    number = 20
    # parenthesis after a integer variable
    number()

    Output

    Traceback (most recent call last):
    File "main.py", line 6, in <module>
    number()
    TypeError: 'int' object is not callable

    Common Scenario

    There are two major common cases when most new Python learners commit this error.

    1. Use the same name for function and integer variables.
    2. Forget to put the multiplication operator while performing a mathematical calculation.

    Scenario 1: Using the same name for int variable and function.

    The most common scenario when Python learners commit this mistake is when they use the same name for the function and the integer variable.

    Let's create an example where we want to calculate the total sum of our bill list. And we use the Python inbuilt function called sum() to calculate the sum of the list and use the same variable name to store the total sum.

    Example

    bill = [12, 34, 23, 53, 10, 9]
    
    # total integer sum
    sum = 0
    
    # using the inbuilt Python sum method
    sum = sum(bill)
    
    print("The total bill is: ",sum)

    Output

    Traceback (most recent call last):
    File "main.py", line 7, in <module>
    sum = sum(bill)
    TypeError: 'int' object is not callable

    Break the code

    We are getting this error because, in line 4, we defined a new integer variable by the name sum = 0 . Now for the complete program, Python will treat the sum as an integer value(until we change it).

    But in line 7, we are trying to compute the sum of the bill list using the Python sum() function, but now Python is confused between the names, so it will treat the sum as the integer object, not as an inbuilt function, and throw the TypeError 'int' object is not callable .

    Solution 1

    The solution to the above problem is very simple. We just need to change the integer sum object name to another name, so the inbuilt function can be invoked.

    Solution

    bill = [12, 34, 23, 53, 10, 9]
    
    # total integer
    total = 0
    
    # using the inbuilt Python sum method
    total = sum(bill)
    
    print("The total bill is: ",total)

    Output

    The total bill is: 141

    Scenario 2: Forget to put the multiplication operator while performing a mathematical calculation

    In mathematics, we use brackets to represent a mathematical expression. There we can use the () brackets to represent the multiplication operation between the numbers inside and outside the bracket.

    for instance (in mathematics)

    2(3+4) = 14

    But this syntax is invalid in Python. In Python, we need to explicitly specify the mathematic operator between the number and opening & closing parenthesis. for instance, if we rewrite the above expression in Python, we have to write it in this manner.

    In Python

    >>>2*(3+4) 
    14

    Example

    a = 2
    b = 3
    c = 4
    
    # error
    result = a(b+c)
    print(result)

    Output

    Traceback (most recent call last):
    File "main.py", line 6, in <module>
    result = a(b+c)
    TypeError: 'int' object is not callable

    Break the code

    In the above example a , b and c all are integer, but in line 6, we are missing the * operator between the a and opening parenthesis ( that's why Python is calling a as a function  that leads to the TypeError: 'int' object is not callable error.

    Solution 3

    The solution for the above error is  very simple, we just need to specify the * multiplication operator between a and ( .

    solution 2

    a = 2
    b = 3
    c = 4
    
    # solved
    result = a+(b+c)
    
    print(result)

    Output

    14

    Conclusion

    In this Python tutorial, we learned what is TypeError: ‘int’ object is not callable Error in Python and how to solve it. If you look closely at the error message, you will get an overall idea of what this error is all about. This error generally occurs in Python when we try to call a function using an integer name. To solve this problem, all you need to do is remove the parenthesis after the integer variable name.

    If you are still getting this error in your Python program, you can comment your code and query in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments