TypeError: 'X' object is not callable
1 · The lesson
readWhat 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
Traceback (most recent call last): File "main.py", line 3, in <module> n = int("42") TypeError: 'int' object is not callable
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.
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:
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:
del int # restores the built-in # or import builtins n = builtins.int("42")
Remove the extra parentheses if the value isn't a function:
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 datetimeand then wrotedatetime = datetime.now(), which replaced the class with a value. - A loop written as
for list in groups:overwrote the built-inlist, so a laterlist(...)call failed. - You wrote
df.shape()in pandas.shapeis 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 methodname(). The instance attribute hides the method, soobj.name()calls a string.
Related errors
- NameError: name 'X' is not defined: the opposite kind of name mistake.
- AttributeError: 'list' object has no attribute 'X': calling methods that don't exist.
- AttributeError: module 'X' has no attribute 'Y'
See Also
- All Python errors: the full index, by type and by when it happens.
- Functions (tutorial)
- Naming conventions cheat sheet
- Classes (tutorial)
Practice this
on practicepython.inShort 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.