The error message:
TypeError: 'int' object is not callable
occurs when you try to call an integer (int
) as if it were a function using parentheses ()
.
1. Causes and Solutions
Cause 1: Using Parentheses Instead of Square Brackets for Indexing
Using ()
instead of []
for accessing list or tuple elements causes this error.
Incorrect Code:
numbers = [10, 20, 30]
print(numbers(0)) # TypeError: 'int' object is not callable
Solution: Use Square Brackets []
for Indexing
print(numbers[0]) # Output: 10
Cause 2: Overwriting a Built-in Function with an Integer
If you assign an integer to a built-in function’s name, Python treats it as an integer.
Incorrect Code:
sum = 100 # Overwrites the built-in sum() function
print(sum([1, 2, 3])) # TypeError
Solution: Avoid Using Built-in Function Names as Variables
total = 100 # Use a different variable name
print(sum([1, 2, 3])) # Output: 6
Cause 3: Accidentally Using Parentheses After an Integer
Trying to call an integer as if it were a function causes this error.
Incorrect Code:
x = 5
print(x()) # TypeError
Solution: Remove Parentheses
print(x) # Output: 5
Cause 4: Using a Number as a Function Name
If a variable name starts with a number, it causes a syntax error, but if a number is mistakenly used later as a function, it triggers this TypeError.
Incorrect Code:
five = 5
five() # TypeError
Solution: Use Meaningful Function Names
def five():
return 5
print(five()) # Output: 5
Cause 5: Forgetting the Multiplication Operator *
If you forget *
when multiplying numbers inside parentheses, Python treats the number as a function.
Incorrect Code:
result = (2)(3) # TypeError
Solution: Use the *
Operator for Multiplication
result = (2 * 3)
print(result) # Output: 6