One error that you might encounter when working with Python is:
TypeError: 'int' object is not callable
This error occurs when you mistakenly call an integer (int
) object as if it was a function.
The following code is an example that causes this error:
900()
There are three possible scenarios that cause this error:
- You attempt to multiply an integer without adding the
*
operator - You override the
int()
function with an int object - You have a function and an integer variable with the same name
This tutorial shows how to reproduce the error in each scenario and how to fix it.
1. You attempt to multiply an int without adding the *
operator
First, you declared a simple int variable as follows:
my_int = 30
Next, you attempt to multiply the int variable with a number acquired from math operations as follows:
x = my_int(5 - 2)
Output:
Traceback (most recent call last):
File "main.py", line 3, in <module>
x = my_int(5 - 2)
^^^^^^^^^^^^^^^
TypeError: 'int' object is not callable
The error occurs because you’re adding parentheses next to an int
object.
While the above is a valid multiplication formula in real life, this makes Python thinks you’re trying to call a function named my_int()
instead of trying to do a multiplication.
To resolve this error, you need to make sure that you’re not adding parentheses next to an int object.
Since you want to multiply the variable, add the multiplication operator *
as follows:
my_int = 30
x = my_int * (5 - 2)
print(x) # 90
This time, the multiplication succeeds and we didn’t receive an error.
2. You override the int()
function with an int object
The built-in function named int()
is used to convert an object of another type into an int object.
For example, here’s how to convert a string into an int:
my_string = "0385"
my_int = int(my_string)
print(my_int) # 385
If you mistakenly create a variable named int
, then the int()
function would be overwritten.
When you call the int()
function, the error would occur as follows:
int = 44
another_int = int("0385")
Output:
Traceback (most recent call last):
File "/main.py", line 3, in <module>
another_int = int("0385")
^^^^^^^^^^^
TypeError: 'int' object is not callable
Because you created a variable named int
, the int()
function gets replaced with that variable, causing you to call an int object instead of the built-in function.
To avoid this error, you need to declare the variable using another name:
my_int = 44 # ✅
another_int = int("0385")
This way, the keyword int
would still point to the int function, and the error won’t be raised.
You have a function and an integer variable with the same name
Suppose you have a function named get_name()
that returns a string as follows:
def get_name():
return "Nathan"
Then down the line, you declared a variable using get_name
as the identifier:
get_name = 867
This cause the get_name
variable to shadow the get_name()
function. When you try to call the function, Python thinks you’re calling the variable instead:
def get_name():
return "Nathan"
get_name = 867
res = get_name() # TypeError: 'int' object is not callable
To resolve this error, make sure that there’s no duplicate name in your source code.
Each function and variable must have a unique name:
def get_name():
return "Nathan"
x = 867
res = get_name() # ✅
Now that the name for the function and the variable are unique, we didn’t receive any error.
Conclusion
The TypeError: 'int' object is not callable
occurs in Python when you call an int object using parentheses like it’s a function.
To resolve this error, you need to make sure that you’re not calling an int object in your source code.
I hope this tutorial helps. Happy coding!