The TypeError: 'int' object is not callable is a common Python error that arises when you try to use an integer value as if it were a function by appending parentheses () to it. Integers represent numerical data and can not be executed or "called" like functions or methods.
This guide explains the various scenarios that lead to this error and provides clear solutions for each.
Understanding the Error: Callable vs. Non-Callable Objects
In Python, parentheses () following a name indicate a call operation,i.e. you're asking Python to execute the code associated with that name (e.g., run a function, call a method, instantiate a class). Objects that can be called are known as "callables". Integers (int), like other simple data types such as strings (str) or booleans (bool), are not callable. They hold data, but they don't represent executable code blocks themselves. Trying to call a non-callable object like an integer results in a TypeError.
my_number = 42
print(type(my_number)) # Output: <class 'int'>
try:
my_number()
except TypeError as e:
print(e)Cause 1: Accidentally Calling an Integer Variable
The simplest cause is having a variable that holds an integer and mistakenly adding parentheses after it, maybe intending to print it or use it in another operation.
count = 10
try:
print(count())
except TypeError as e:
print(e)
print(count) # Output: 10Cause 2: Missing Mathematical Operator
This error can uniquely occur with numbers when you implicitly try to multiply by juxtaposition (like in mathematical notation 2x) but forget the actual multiplication operator (*). Python interprets number(expression) as attempting to call number.
try:
result = 2 (5 + 3)
print(result)
except TypeError as e:
print(e) # Output: 'int' object is not callable
result_multiply = 2 * (5 + 3)
print(f"Multiplication: {result_multiply}") # Output: Multiplication: 16
result_add = 2 + (5 + 3)
print(f"Addition: {result_add}") # Output: Addition: 10Ensure all mathematical operations have explicit operators (+, -, *, /, etc.).
Cause 3: Variable Name Clashing with Function Name
Assigning an integer to a variable that has the same name as a previously defined function will hide the function. Subsequent attempts to call the function by that name will instead try (and fail) to call the integer value stored in the variable.
def calculate_score():
"""Calculates a score."""
print("Original calculate_score function...")
return 100
score = calculate_score()
print(f"Initial score: {score}")
calculate_score = 50 # Now 'calculate_score' refers to the integer 50
try:
new_score = calculate_score()
print(new_score)
except TypeError as e:
print(f"Error: {e}")
def calculate_score_func(): # Renamed function
print("Original calculate_score_func...")
return 100
current_score_value = 50 # Use a different variable name
new_score_fixed = calculate_score_func()
print(f"Fixed call result: {new_score_fixed}") # Output: 100Cause 4: Shadowing the Built-in int Type
If you name a variable int, you override Python's built-in int() type constructor. Later attempts to use int() (e.g., to convert a string to an integer) will mistakenly try to call your integer variable.
int = 123
print(f"The variable 'int' now holds: {int}")
try:
number_from_string = int("456")
print(number_from_string)
except TypeError as e:
print(e)
my_integer_variable = 123
number_from_string_fixed = int("456") # Correctly converts string '456' to integer 456
print(f"Using built-in int(): {number_from_string_fixed}") # Output: 456Cause 5: Class Attribute Name Clashing with Method Name
Within a class, an instance attribute and a method sharing the same name can lead to this error if the attribute holds an integer. Accessing the name on an instance typically retrieves the attribute, hiding the method.
class Counter:
def __init__(self, initial_value=0):
self.value = initial_value # Holds an integer
def value(self):
print("Method 'value' called!") # This is hard to reach
return self.value * 10 # Example method logic
counter_instance = Counter(5)
print(f"Attribute value: {counter_instance.value}") # Accesses the integer attribute -> 5
try:
result = counter_instance.value()
print(result)
except TypeError as e:
print(f"Error: {e}")
class CounterFixed:
def __init__(self, initial_value=0):
self._value = initial_value # Convention: prefix with _
def get_value(self):
print("Method 'get_value' called!")
return self._value
counter_fixed = CounterFixed(5)
result_fixed = counter_fixed.get_value()
print(f"Fixed call result: {result_fixed}") # Output: 5Cause 6: Calling a Function That Returns an Integer Twice
If a function returns an integer, calling it once works correctly. Adding a second set of parentheses attempts to call the integer that was returned by the first call.
def get_item_count():
print("Getting count...")
return 42 # Returns an integer
try:
count = get_item_count()()
print(count)
except TypeError as e:
print(f"Error: {e}")
count = get_item_count()
print(f"Count: {count}") # Output: 42Debugging Tip: Check Variable Types
When this error occurs, the quickest way to confirm the cause is often to check the type of the object you are trying to call right before the error line:
variable_causing_error = ... # The name involved in the error
print(f"Type before call: {type(variable_causing_error)}")
try:
variable_causing_error()
except TypeError as e:
print(f"Error confirms type issue: {e}")If the type is <class 'int'>, you've found the immediate problem and need to apply the relevant solution from above.
Conclusion
The TypeError: 'int' object is not callable occurs when you incorrectly use call parentheses () on an integer value. Key solutions include:
- Removing parentheses from integer variables when just accessing their value.
- Adding missing mathematical operators (like
*) if multiplication by juxtaposition was intended. - Using distinct names for variables and functions/methods/types to prevent shadowing.
- Calling functions that return integers only once.
By understanding that integers are data, not executable functions, and by being mindful of potential name clashes, you can easily avoid and fix this common TypeError.