Solve - TypeError: 'int' object is not callable in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Romain Robe

Solve - TypeError: 'int' object is not callable in Python #

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.

typeerror int object is not callable

Here is an example of how the error occurs.

main.py
example = 100 # ⛔️ TypeError: 'int' object is not callable example()
You have to track down where the variable got assigned an integer value and correct the assignment to a function or class, or remove the parenthesis.

Make sure you aren't overriding the built-in int() function.

main.py
# 👇️ 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.

main.py
# ✅ 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.

main.py
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.

main.py
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.

main.py
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.

main.py
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.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
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.

Use the search field on my Home Page to filter through my more than 1,000 articles.