How to Fix TypeError: 'int' object is not callable in Python
In this tutorial, you'll learn about How to Fix TypeError: 'int' object is not callable in Python. We cover key concepts, practical examples, and best practices.
Python raises TypeError: 'int' object is not callable when you try to call an integer as if it were a function -- usually because a variable named max, sum, or int overwrites the built-in function.
The Wrong Way
max = 10
print(max(5, 8))
This produces:
Traceback (most recent call last):
File "script.py", line 2, in <module>
print(max(5, 8))
TypeError: 'int' object is not callable
The Right Way
Avoid naming variables after built-in functions:
max_value = 10
print(max(5, 8))
Output:
8
Step-by-Step Fix
1. Identify the shadowing variable
Search your code for variable names that match built-in functions: max, min, sum, int, str, list, dict, set, type, input, print, len, range, zip, map, filter, sorted, reversed, open, file, id, dir.
# Wrong
list = [1, 2, 3]
new_list = list(range(5)) # TypeError
# Right
my_list = [1, 2, 3]
new_list = list(range(5))
print(new_list)
Output:
[0, 1, 2, 3, 4]
2. Restart the interpreter if needed
If you accidentally overwrote a built-in in an interactive session, restart the interpreter or delete the shadowing variable:
del max
3. Use a linter to catch shadowing
pip install flake8
flake8 script.py
Flake8 reports E741 for ambiguous variable names and can help catch shadowing with custom rules.
4. Check for missing operators
# Wrong -- missing multiplication operator
result = 5(3 + 2)
# Right
result = 5 * (3 + 2)
Prevention Tips
- Never name a variable after a Python built-in function.
- Use a linter with shadowing detection enabled.
- Use IDE features that highlight when you override a built-in.
- Adopt naming conventions like
max_value,str_contentinstead ofmax,str. - Run
python3 -W error::DeprecationWarningin CI to catch issues early.
Common Mistakes with typeerror
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro