Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Romain Robe
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()
Make sure you aren't overriding the built-in int()
function.
# 👇️ overrides 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 primitive and
try to call it as a function.
To solve the error, make sure to rename your variable and restart your script.
# ✅ Not overriding built-in int() anymore my_int = 150 print(int('150')) # 👉️ 150
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 parenthesis
Another common cause of the error is having a function and a variable that share the same name.
def example(): return 'hello world' 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 'hello world' example_2 = 100 print(example()) # 👉️ "hello world"
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.