How to fix “TypeError: ‘int’ object is not callable” in Python
The Python’s “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 of this type error.
🎧 Debugging Jam
Calling all coders in need of a rhythm boost! Tune in to our 24/7 Lofi Coding Radio on YouTube, and let's code to the beat – subscribe for the ultimate coding groove!" Let the bug-hunting begin! 🎵💻🚀
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 type error:
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.
Psssst! Do you want to learn web development in 2023?
How to fix it?
This error happens under various scenarios:
- 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
- Missing a mathematical operator
Let's explore 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, 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 complain with a type error 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
- Explicitly access the
sumfunction from the builtins module (
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
# 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 = 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 = 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
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 (e.g., 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())
In the above example, 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 type error.
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
a can become
× (b + c)
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 (integer in this case), it'll raise the type error.
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
Alright, I think it does it! I hope this quick guide helped you fix your problem.
Thanks for reading.
Never miss a guide like this!
Disclaimer: This post may contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.