The Python “TypeError: ‘int’ object is not callable” error occurs when you try to call an integer (int object) as if it was a function. Overriding functions, and calling them later on, is the most common cause for this TypeError.
TypeError: ‘Int’ Object Is Not Callable Solved
The most common cause for TypeError: ‘int’ object is not callable is when you declare a variable with a name that matches the name of a function. Such as defining a variable named sum
and calling sum()
:
# this overrides the sum value to 0
sum = 0
values = [34, 43, 2, 8, 1]
# ⛔ Raises TypeError: 'int' object is not callable
sum = sum(values)
The best solution is to rename the variable, in this case sum
, as follows:
sum_of_values = 0
values = [34, 43, 2, 8, 1]
sum_of_values = sum(values)
print(sum_of_values)
# output: 88
Here’s what the error message looks like:
Traceback (most recent call last):
File "/dwd/sandbox/test.py", line 4, in
round = round(result)
^^^^^^^^^^^^^
TypeError: 'int' object is not callable
In the simplest terms, this is what happens:
result = 33 / 5
round = round(result)
# ⚠️ The value of round is 7 from now on
# it's no longer pointing to the round() built-in function
result = 34 / 8
# ⛔ Calling round at this point is equivalent to calling 7()
round = round(result)
Additionally, if you accidentally put an extra parenthesis after a function that returns an integer, you’ll get the same TypeError:
print(round(14.5)())
In the above example, the round()
function returns an int
value, and having an extra pair of parenthesis means calling the return integer value like function.
What Causes the TypeError: ‘Int’ Object Is Not Callable?
There are four common scenarios that create the “‘int’ object is not callable” error in Python. This includes:
- Declaring variable with a name that’s also the name of a function.
- Calling a method that’s also the name of a property.
- Calling a method decorated with @property.
- Missing a mathematical operator.
How to Solve TypeError “‘Int’ Object Is Not Callable” in Python
Let’s explore how to solve each scenario with some examples.
1. Declaring a Variable With a Name That’s Also the Name of a Function
A Python function is an object like any other built-in object, such as int
, float
, dict
and list
, etc.
All built-in functions are defined in the builtins module and assigned a global name for easier access. For instance, a call to sum()
invokes the __builtins__.sum()
function internally.
That said, overriding a function, accidentally or on purpose, with another value is technically possible.
For instance, if you define a variable named sum
and initialize it with an integer value, sum()
will no longer be a function.
# this overrides the sum value to 0
sum = 0
values = [34, 43, 2, 8, 1]
# ⛔ Raises TypeError: 'int' object is not callable
sum = sum(values)
If you run the above code, Python will produce a TypeError because 0, the new value of sum, isn’t callable.
This is the first thing to check if you’re calling a function, and you get this error.
You have two ways to fix the issue:
- Rename the variable
sum
. - Explicitly access the
sum
function from the builtins module (__bultins__.sum
).
The second approach isn’t recommended unless you’re developing a module. For instance, if you want to implement an open()
function that wraps the built-in open():
# Custom open() function using the built-in open() internally
def open(filename):
# ...
__builtins__.open(filename, 'w', opener=opener)
# ...
In almost every other case, you should always avoid naming your variables as existing functions and methods. But if you’ve done so, renaming the variable would solve the issue.
So, the above example could be fixed like this:
sum_of_values = 0
values = [34, 43, 2, 8, 1]
sum_of_values = sum(values)
print(sum_of_values)
# output: 88
Here’s another example with the built-in max() function:
max = 0
items = [1, 45, 54, 165, 0, 2]
# ⛔ Raises the type error
max = max(items)
To fix it, we rename the max
variable name to max_value
:
max_value = 0
items = [1, 45, 54, 165, 0, 2]
max_value = max(items)
print('The biggest number is:', max_value)
# output: The biggest number is: 165
Long story short, you should never use a function name (built-in or user-defined) for your variables.
Now, let’s get to the less common mistakes that lead to this error.
2. Calling a Method That’s Also the Name of a Property
When you define a property in a class constructor, any further definitions of the same name, such as methods, will be ignored.
class Book:
def __init__(self, book_code, book_title):
self.title = book_title
self.code = book_code
def code(self):
return self.code
book = Book(1, 'Head First Python')
# ⛔ Raises TypeError: 'int' object is not callable
print(book.code())
Since we have a property named code, the method code()
is ignored. As a result, any reference to the code will return the property code. Obviously, calling code()
is like calling 1()
, which raises the TypeError.
To fix it, we need to change the method name:
class Book:
def __init__(self, book_code, book_title):
self.title = book_title
self.code = book_code
def get_code(self):
return self.code
book = Book(1, 'Head First Python')
print(book.get_code())
3. Calling a Method Decorated With @property Decorator
The @property
decorator turns a method into a “getter” for a read-only attribute of the same name.
class Book:
def __init__(self, book_code, book_title):
self._title = book_title
self._code = book_code
@property
def code(self):
"""Get the book code"""
return self._code
book = Book(1, 'Head First Python')
# ⛔ Raises the type error
print(book.code())
You need to access the getter method without the parentheses:
book = Book(1, 'Head First Python')
print(book.code)
# Output: 1
4. Missing a Mathematical Operator
In algebra, we can remove the multiplication operator to avoid ambiguity in our expressions. For instance, a × b
, can be ab, or a × (b + c)
can become a(b + c)
.
But not in Python!
In the above example, if you remove the multiplication operator in a × (b + c)
, Python’s interpreter would consider it a function call. And since the value of a is numeric, an integer in this case, it’ll raise the TypeError.
So, if you have something like this in your code:
a = 12
b = 3
c = 6
# ⛔ raises the type error
result = a (b + c)
You’d have to change it like so:
a = 12
b = 3
c = 6
result = a * (b + c)
print(result)
# output: 108
Problem solved.
Frequently Asked Questions
What does the TypeError: ‘int’ object is not callable mean in Python?
Python produces the TypeError: ‘int’ object is not callable when you try to call an integer (int object) as if it was a function. This most often occurs when you override a function and then attempt to call it later on. The error looks like this:
Traceback (most recent call last):
File "/dwd/sandbox/test.py", line 4, in
round = round(result)
^^^^^^^^^^^^^
TypeError: 'int' object is not callable
How do you fix the TypeError: ‘int’ object is not callable in Python?
The most common cause for TypeError: ‘int’ object is when a variable is declared with a name that matches the name of a function. There are two ways to fix it:
- Rename the variable
sum
. - Explicitly access the sum function from the builtins module (
__bultins__.sum
).