PythonMastery
reference 3 min read · lesson 5 of 45 in Errors

TypeError: 'X' object is not callable

1 · The lesson

read

What this error means

You put () after something that isn't a function. Python tried to call the value — like invoking a function — but the value is an int, list, str, or some other non-callable object. The most common cause is shadowing a built-in: you assigned a value to a name like list, int, str, or sum, and then later tried to use that built-in as a function.

When you see it

python
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    n = int("42")
TypeError: 'int' object is not callable
python
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    print(total())
TypeError: 'int' object is not callable

Why it happens

Three usual suspects:

1. You shadowed a built-in. Earlier code did int = 5 or list = [1, 2], so int(...) and list(...) now refer to your value, not the built-in function.
2. You added stray parentheses. You wrote result() when result is already the value, not a function returning the value.
3. You named a variable the same as a function in the same scope, so the function got overwritten.

python
int = 5                # shadowed the built-in
n = int("42")          # TypeError: 'int' object is not callable

total = sum([1, 2, 3])
print(total())         # total is 6, not a function

How to fix it

Stop using built-in names as variables. Rename your variable:

python
count = 5              # not `int = 5`
items = [1, 2, 3]      # not `list = [1, 2, 3]`
total = 6              # don't shadow `sum`

If you've already shadowed a built-in inside the REPL or a Jupyter notebook, recover it:

python
del int                # restores the built-in
# or
import builtins
n = builtins.int("42")

Drop the extra parentheses if the value isn't a function:

python
total = sum([1, 2, 3])
print(total)           # not total()

Watch out for class instances without __call__. Calling an instance like obj() only works if the class defines __call__. Use obj.method() to call a method instead.

When you'd actually see this in real code

  • You imported from datetime import datetime and then wrote datetime = datetime.now(), blowing away the class.
  • A loop variable list overwrote the built-in inside a comprehension.
  • You assigned df = df.head (forgetting the parentheses) and then called df() later.
  • A library's API was renamed: requests.session is the class, requests.Session() is how to instantiate it.

See Also

Practice this

on practicepython.in

Short exercises that run in your browser and tell you what your code actually did, not just whether a test passed.