Typeerror: int object is not callable – How to Fix in Python
As a Python developer, you‘ve likely encountered the infamous "Typeerror: int object is not callable" error at some point. This error message can be confusing and frustrating, especially for beginners. But fear not! In this comprehensive guide, we‘ll break down exactly what causes this error and how you can fix it in your Python code.
What causes the "Typeerror: int object is not callable" error?
The "Typeerror: int object is not callable" error occurs in a few different scenarios:
- When you use a reserved keyword or built-in function name like int, str, or list as a variable name
- When you forget to include parentheses () when calling a function
- When you try to call an integer as if it were a function
Let‘s look at code examples that demonstrate each of these causes:
Using reserved keywords or built-in function names as variables
One common mistake is using a built-in function name as a variable, like this:
int = 42 print(int(3.14))
Output:
Traceback (most recent call last): File "example.py", line 2, in print(int(3.14)) TypeError: ‘int‘ object is not callable
Here, we‘ve used int as a variable name, which overwrites the built-in int() function. When we later try to use int() to convert a float to an integer, Python throws the error because int is no longer a callable function, but an integer object.
Forgetting parentheses when calling functions
Another easy mistake is omitting the parentheses when calling a function:
def greet(name): return f"Hello, {name}!"greeting = greet print(greeting)
Output:
Traceback (most recent call last): File "example.py", line 5, in print(greeting) TypeError: ‘function‘ object is not callable
In this example, we assign the greet function to the variable greeting. However, when we try to print greeting, we get an error because we‘re missing the parentheses needed to actually call the function.
Attempting to call an integer
Occasionally, you might accidentally try to use an integer as a function:
x = 5 y = x(3) print(y)
Output:
Traceback (most recent call last): File "example.py", line 2, in y = x(3) TypeError: ‘int‘ object is not callable
Integers are not callable in Python, so trying to use one like a function x(3) results in the "int object is not callable" error.
How to fix "Typeerror: int object is not callable" for built-in names
If you get this error from using a built-in function name as a variable, you have two options to fix it:
- Rename your variable to something that doesn‘t conflict with the built-in name
- Access the built-in function through the builtins module
Renaming the variable
The simplest solution is to rename your variable to something more descriptive that doesn‘t clash with any reserved keywords or built-in functions in Python:
my_int = 42 print(int(3.14))
Output:
3
Accessing built-ins through the builtins module
Alternatively, you can explicitly access the int function through Python‘s builtins module:
import builtinsint = 42 print(builtins.int(3.14))
Output:
3
This way, you can still use int as a variable name if needed while retaining access to the original int() function.
How to fix "Typeerror: int object is not callable" for missing parentheses
When you see this error because you forgot parentheses on a function call, the fix is simple – just add them in!
def greet(name): return f"Hello, {name}!"greeting = greet("Alice")
print(greeting)
Output:
Hello, Alice!
By adding parentheses and passing the expected name argument, greeting now holds the return value of the greet() function call, which we can print successfully.
How to fix "Typeerror: int object is not callable" when calling integers
Remember, integers are data objects in Python, not callable functions. To perform mathematical operations, use the proper arithmetic operators:
x = 5 y = x + 3 print(y)
Output:
8
You can also use other operators like -, *, and / for subtraction, multiplication and division respectively. Just be sure you‘re always operating on numeric data types.
Tips to avoid "Typeerror: int object is not callable" errors
While these fixes are straightforward, it‘s better to avoid "Typeerror: int object is not callable" errors in the first place! Here are some tips:
-
Be cautious of naming variables after built-in functions in Python. If you do, access the original function through the builtins module.
-
Double-check that you‘ve included parentheses () when calling functions. IDEs with syntax highlighting can help catch these omissions.
-
Make sure you understand which data types are callable (like functions) and which are not (like integers). Treat them accordingly.
-
Consider using a linter or IDE with robust code completion to surface potential errors before you run your code. They can alert you to issues like redefining built-ins.
Conclusion
The "Typeerror: int object is not callable" error in Python can seem daunting at first, but it usually boils down to a few common causes:
- Redefining built-in function names as variables
- Omitting required parentheses on function calls
- Trying to call non-callable objects like integers
Luckily, the fixes are just as straightforward – either rename your variables, add in missing parentheses, or use the correct syntax for your data type. Adopting some best practices like deliberately naming variables and using code analysis tools can help prevent these errors too.
Armed with this knowledge, you‘re well-equipped to tackle any "Typeerror: int object is not callable" errors in your Python code. Happy coding!