TypeError: object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: object is not callable in Python #

The Python "TypeError: object is not callable" occurs when we try to call a not-callable object (e.g. a list or dict) as a function using parenthesis (). To solve the error, make sure to use square brackets when accessing a list at index or a dictionary's key, e.g. my_list[0].

typeerror object is not callable

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ TypeError: 'list' object is not callable print(my_list(0)) # 👈️ uses parentheses instead of square brackets

The issue in the code sample is that we used parentheses instead of square brackets when accessing the list at a specific index.

main.py
my_list = ['a', 'b', 'c'] print(my_list[0]) # 👉️ "a" my_dict = {'name': 'Alice'} print(my_dict['name']) # 👉️ "Alice"

The error occurs when we try to call a not-callable object as a function (using parentheses).

For example, functions, classes and class methods are callable. Integers, strings, lists, dictionaries, set objects, etc are not callable.

You might have an extra set of parentheses that you have to remove to solve the error.

main.py
my_bool = False # ⛔️ TypeError: 'bool' object is not callable print(my_bool())

Removing the parentheses after the non-callable object would solve the issue.

If you are working with a class, make sure the class doesn't have an attribute and a method with the same name.
main.py
class Employee(): def __init__(self, name): # 👇️ this attribute hides the method self.name = name def name(self): return self.name emp = Employee('Alice') # ⛔️ TypeError: 'str' object is not callable print(emp.name())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to make sure to rename the method in the class.

main.py
class Employee(): def __init__(self, name): self.name = name def get_name(self): return self.name emp = Employee('Alice') print(emp.get_name()) # 👉️ "Alice"

Once you rename the method, you will be able to call it without any issues.

Also, make sure you don't have a global variable and a function with the same name.
main.py
str = 'hello world' # ⛔️ TypeError: 'str' object is not callable print(str(10))

We override the built-in str function setting it to a string primitive and try to call it as a function.

To solve the error in this situation, make sure to rename your variable and restart your script.

main.py
# ✅ Not overriding built-in str() anymore my_str = 'hello world' print(str(10))

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'hello world' example = 'hi' # ⛔️ TypeError: 'str' object is not callable print(example())

The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

If you got the error when importing a module and trying to call it, you have to correct your import statement or access an attribute on the module that is a function.

Imagine we have 2 files - another.py and main.py.

another.py
def do_math(a, b): return a + b

And here is the content for main.py.

main.py
import another # # TypeError: 'module' object is not callable another(10, 15)

The issue is that, in the main.py file, we import the another module but use parentheses to try to call the do_math function.

One way to solve the error is to use dot notation to get access to the function before we try to call it.
main.py
import another print(type(another.do_math)) # 👉️ <class 'function'> print(another.do_math(10, 15)) # 👉️ 25

We used dot notation to get access to the function from another.py before trying to call it.

You can use the type() function to check if the specific attribute points to a function or a class before calling it.

Alternatively, you can import the function directly from the another.py module.

main.py
from another import do_math print(do_math(10, 15)) # 👉️ 25

This import statement only imports the my_dict variable from the another.py module.

If you need to import multiple variables or functions, separate them by commas in the import statement, e.g. from another import first, second, third

Conclusion #

The Python "TypeError: object is not callable" occurs when we try to call a not-callable object (e.g. a list or dict) as a function using parenthesis (). To solve the error, make sure to use square brackets when accessing a list at index or a dictionary's key, e.g. my_list[0].

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.