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 — 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
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 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.
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:
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:
del int # restores the built-in # or import builtins n = builtins.int("42")
Drop the extra parentheses if the value isn't a function:
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 datetimeand then wrotedatetime = datetime.now(), blowing away the class. - A loop variable
listoverwrote the built-in inside a comprehension. - You assigned
df = df.head(forgetting the parentheses) and then calleddf()later. - A library's API was renamed:
requests.sessionis the class,requests.Session()is how to instantiate it.
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 actually did, not just whether a test passed.