How to Solve Python TypeError: ‘int’ object is not callable

by | Programming, Python, Tips

If you try to call an integer as if it were a function, you will raise the error “TypeError: ‘int’ object is not callable”.

To solve this error, ensure that you do not override names for built-in functions like int() or round(). For example,

my_int = int("4")
print(my_int)

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: ‘int’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

Integers do not respond to a function call because they are not functions. If you try to call a int object as if it were a function, you will raise the TypeError: ‘int’ object is not callable.

We can check if an object is callable by passing it to the built-in callable() method. If the method returns True, then the object is callable. Otherwise, if it returns False the object is not callable. Let’s look at evaluating an int object with the callable method:

my_int = 10
print(type(my_int))
print(callable(my_int))
<class 'int'>
False

The callable function returns False for the int object.

Example

Let’s look at an example of attempting to call an int object. First, we will define a floating-point number.

my_float = 3.6

Next, we will round the float to the nearest integer using the built-in round() function:

int = round(my_float)
print(int)
4

Next, we will try to convert a numerical string to an integer using the built-in int() function.

my_int = int("17")
print(my_int)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 my_int = int("17")
      2 print(my_int)

TypeError: 'int' object is not callable

The error occurs because we assigned the first int object to the variable name int, which overrides the reserved name for the built-in function. Then when we try to convert the numerical string to an integer, we are calling the int object instead.

We can verify the object type using the built-in type() function:

print(type(int))
<class 'int'>

The int variable holds an int object.

Solution

We can solve the error by deleting the variable int using del, then recreate the int object with a different name that is not reserved for built-in functions.

del int
rounded_val = round(my_float)
print(rounded_val)
4

Next, we can convert the numerical string to an integer using the built-in function int() because we did not override it.

my_int = int("17")
print(my_int)

Let’s run the code to get the result:

17

Summary

Congratulations on reading to the end of this tutorial!

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!