TypeError: 'int' object is not callable in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# 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 parentheses.

# Overriding the built-in int() function

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

main.py
# ๐Ÿ‘‡๏ธ Overrides the built-in int() function int = 150 # โ›”๏ธ TypeError: 'int' object is not callable print(int('150'))

overriding built in int function

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.

main.py
# โœ… 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.

# Missing a mathematical operator

The error also occurs when you have a missing mathematical operator.

main.py
# โ›”๏ธ 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.

main.py
# โœ… 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

specify the missing operator

Now the interpreter knows that we aren't trying to invoke the integer value 2.

# Having a function and a variable with the same name

Another common cause of the error is having a function and a variable that share the same name.

main.py
def example(): return 'bobbyhadz.com' example = 100 # โ›”๏ธ TypeError: 'int' object is not callable example()

having function and variable with the same name

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 'bobbyhadz.com' my_int = 100 print(example()) # ๐Ÿ‘‰๏ธ "bobbyhadz.com"

We gave the variable a different name, so it no longer clashes with the function.

# Common causes of the error

The error occurs for multiple reasons:

  • Trying to call an integer value with parentheses ().
  • Having a missing mathematical operator.
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to an integer.
  • Having a class method and a class property with the same name.
  • Calling a function that returns an integer value twice.

# Having a class method and property with the same name

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()

class method and property with the same name

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.

# Trying to call a function that returns an integer twice

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

main.py
def get_num(): return 100 print(get_num()) # ๐Ÿ‘‰๏ธ 100

# Conclusion

To solve the "TypeError: 'int' object is not callable" error, make sure:

  • You aren't trying to call an integer with parentheses.
  • You don't have a missing mathematical operator.
  • You don't have a function and a variable with the same name.
  • You aren't overriding a built-in function and setting it to a boolean.
  • You don't have a class method and a property with the same name.
  • You aren't calling a function that returns an integer twice.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev