Skip to content

/errors/python/typeerror-int-object-is-not-callable

TypeError: 'int' object is not callable

verified onenvironmentlocal · linux 6.18.44-fc-v153 x64 · GNU bash 5.2.21 · Python 3.11.17updated
tool
Python
exit code
1
versions
Python 3.x
os
any

TypeError: 'int' object is not callable means the code put parentheses after a value that is a number. Python only lets you call functions, classes and other callable objects, so 5(...) fails. The most common cause is a variable that shadows a built-in: after sum = 10, the name sum no longer refers to the built-in function, and the next sum(items) tries to call the integer 10.

Reproduce it

bash
python3 - <<'PY'
sum = 10
print(sum([1, 2, 3]))
PY

Output · exit 1

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'int' object is not callable

The traceback names the line with the call and ends with the type of the object that was called. The type in quotes changes with the value: 'int', 'str', 'float', 'list' or 'dict'. Everything below applies to all of them.

Cause 1: a variable shadows a built-in

Python looks up a name in the local scope first, then the enclosing scopes, the module and finally the built-ins. An assignment creates a name in the current scope, so it hides the built-in with the same name for the rest of that scope. Typical victims are sum, max, min, len, int, id, input, list, dict, str and type. Counters and accumulators are the usual way it happens.

Recognise it: the failing line calls a well-known built-in, and an earlier line in the same file assigns a number to that name.

bash
python3 - <<'PY'
import builtins
total = 10
print(sum([1, 2, 3]))
print(callable(builtins.sum), callable(total))
PY

Output

6
True False

Renaming the variable fixes it, and callable() tells you at a glance whether a name can be called.

Cause 2: a missing operator

Mathematical notation allows 2(3 + 4). Python does not insert a multiplication sign, it reads the expression as a call of 2. Python 3.8 and later also emits a SyntaxWarning: 'int' object is not callable; perhaps you missed a comma? for literals. Write 2 * (3 + 4).

Cause 3: a parameter or attribute with the same name as a function

A function parameter called max or len hides the built-in inside that function. A class that stores self.count = 0 and also defines a method count makes self.count() fail, because the instance attribute wins over the method. Give the data and the method different names.

Fix it

  1. Read the line in the traceback and find the name followed by (.
  2. Search upward for an assignment to that name.
  3. Rename the assigned variable. If the name was assigned in an interactive session, run del name to expose the built-in again.
  4. Run the code again to confirm that the call now reaches the function.
bash
python3 - <<'PY'
sum = 10
del sum
print(sum([1, 2, 3]))
PY

Output

6

Prevent it

Avoid names from dir(builtins) for your own variables, and use descriptive names such as total, largest or item_count. Linters (ruff rule A001 and flake8-builtins) flag assignments that shadow built-ins before the code runs.

FAQ

Why does the error appear only the second time I run my code in Jupyter?

The first run assigned a number to a name like sum. That assignment stays in the kernel, so the next call to sum(...) fails. Restart the kernel or run del sum.

Does the same error happen for str, list or float objects?

Yes. The text inside the quotes is the type of the object that was called, so you also see 'str' object is not callable or 'list' object is not callable. The cause and the fix are the same.

How do I find which line shadows the name?

Search the file for an assignment to the name before the failing call, or run python -W error with a linter such as pyflakes or ruff, which warns about redefined built-ins.

Sources