PythonMastery
reference 3 min read · lesson 5 of 60 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, the way you call 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 causes:

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                # hides the built-in int()
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

Don't use 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, you can get it back:

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

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

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

Be careful with class instances that have no __call__ method. Calling an instance like obj() only works if the class defines __call__. Use obj.method() to call a method instead.

When you'd see this in real code

  • You imported from datetime import datetime and then wrote datetime = datetime.now(), which replaced the class with a value.
  • A loop written as for list in groups: overwrote the built-in list, so a later list(...) call failed.
  • You wrote df.shape() in pandas. shape is an attribute that holds a tuple, so this gives 'tuple' object is not callable.
  • A class sets self.name = ... in __init__ and also defines a method name(). The instance attribute hides the method, so obj.name() calls a string.

See Also

Practice this

on practicepython.in

Short exercises that run in your browser and tell you what your code did, as well as whether a test passed.

Found this useful? Share it with a friend who’s learning Python.