Last updated: Apr 8, 2024
Reading timeยท4 min
The Python "TypeError: 'int' object is not callable" occurs when we try to call an integer value as a function.
To solve the error, correct the assignment, make sure not to override the
built-in int()
function and resolve any clashes between function and variable
names.
Here is an example of how the error occurs.
example = 100 # โ๏ธ TypeError: 'int' object is not callable example()
int()
functionMake sure you aren't overriding the built-in int() function.
# ๐๏ธ Overrides the built-in int() function int = 150 # โ๏ธ TypeError: 'int' object is not callable print(int('150'))
We override the built-in int
function setting it to an integer value and try
to call the integer as a function.
You can solve the error by renaming the variable.
# โ Not overriding the built-in int() anymore my_int = 150 print(int('150')) # ๐๏ธ 150
The variable no longer clashes with the built-in int()
function, so the issue
is resolved.
Make sure you don't have a variable that shadows built-in functions such as
int
, sum, min
, max
, etc.
The error also occurs when you have a missing mathematical operator.
# โ๏ธ TypeError: 'int' object is not callable result = 2 (5 * 5)
There is no operating between the 2
and the set of parentheses, so the Python
interpreter assumes that we are trying to call the integer 2
.
To solve the error, specify the missing operator.
# โ multiplication result = 2 * (5 * 5) print(result) # ๐๏ธ 50 # โ division result = 2 / (5 * 5) print(result) # ๐๏ธ 0.08 # โ subtraction result = 2 - (5 * 5) print(result) # ๐๏ธ -23 # โ addition result = 2 + (5 * 5) print(result) # ๐๏ธ 27
Now the interpreter knows that we aren't trying to invoke the integer value 2
.
Another common cause of the error is having a function and a variable that share the same name.
def example(): return 'bobbyhadz.com' example = 100 # โ๏ธ TypeError: 'int' object is not callable example()
The example
variable shadows the function with the same name, so when we try
to call the function, we actually end up calling the variable.
Renaming the variable or the function solves the error.
def example(): return 'bobbyhadz.com' my_int = 100 print(example()) # ๐๏ธ "bobbyhadz.com"
We gave the variable a different name, so it no longer clashes with the function.
The error occurs for multiple reasons:
()
.The error is also caused when we have a class method and a class property with the same name.
class Employee: def __init__(self, salary): self.salary = salary # ๐๏ธ Same name as class variable def salary(self): return self.salary emp = Employee(100) # โ๏ธ TypeError: 'int' object is not callable emp.salary()
The Employee
class has a method and an attribute with the same name.
To solve the error, you have to rename the class method.
class Employee: def __init__(self, salary): self.salary = salary def get_salary(self): return self.salary emp = Employee(100) print(emp.get_salary()) # ๐๏ธ 100
Once you rename the method, you will be able to call it without any issues.
Make sure you aren't trying to call a function that returns an integer twice.
def get_num(): return 100 # โ๏ธ TypeError: 'int' object is not callable get_num()() # ๐๏ธ remove second set of parentheses
Notice that we used two sets of parentheses when calling the get_num
function.
The first set of parentheses calls the function and the function returns an integer.
The second set of parentheses tries to call the integer and causes the error.
To solve the error, remove the second set of parentheses.
def get_num(): return 100 print(get_num()) # ๐๏ธ 100
To solve the "TypeError: 'int' object is not callable" error, make sure:
You can learn more about the related topics by checking out the following tutorials: