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
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
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
int = round(my_float) print(int)
Next, we will try to convert a numerical string to an integer using the built-in
my_int = int("17") print(my_int)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In , 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
The int variable holds an
We can solve the error by deleting the variable
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)
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:
Congratulations on reading to the end of this tutorial!
For further reading on not callable TypeErrors, go to the articles:
- How to Solve Python TypeError: ‘bool’ object is not callable.
- How to Solve Python TypeError: ‘Series’ object is not callable
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!