Fix - TypeError: 'dict' object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

Fix - TypeError: 'dict' object is not callable in Python #

The Python "TypeError: 'dict' object is not callable" occurs when we try to call a dictionary as if it were a function. To solve the error, make sure to use square brackets when accessing a key in a dictionary, e.g. my_dict['my_key'].

typeerror dict object is not callable

Here is one example of how the error occurs.

main.py
my_dict = {'name': 'Alice', 'age': 30} # ⛔️ TypeError: 'dict' object is not callable print(my_dict('name')) # 👈️ uses parenthesis instead of square brackets

The issue in the code sample is that we used parenthesis instead of square brackets when accessing a key in the dictionary.

main.py
my_dict = {'name': 'Alice', 'age': 30} # ✅ use square brackets print(my_dict['name']) # 👉️ "Alice" print(my_dict['age']) # 👉️ 30

Another thing to look out for is that you're not overriding the built-in dict() function.

main.py
# 👇️ overrides built-in dict function dict = {'name': 'Alice', 'age': 30} # ⛔️ TypeError: 'dict' object is not callable print(dict(name='Alice', age=30))

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

main.py
# ✅ not overriding built-in dict anymore my_dict = {'name': 'Alice', 'age': 30} print(dict(name='Alice', age=30))

Another common cause of the error is simply trying to call a dict as a function.

main.py
example = {'name': 'Alice', 'age': 30} # ⛔️ TypeError: 'dict' object is not callable example()
To solve the error, you either have to remove the parenthesis or figure out how the variable got assigned a dictionary instead of a function or a class.

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

main.py
def example(): return 'hello world' # 👇️ shadowing function example = {'name': 'Alice', 'age': 30} # ⛔️ TypeError: 'dict' object is not callable 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.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, address): # 👇️ this attribute hides the method self.address = address # 👇️ same name as class variable def address(self): return self.address emp = Employee({'country': 'Austria', 'city': 'Linz'}) # ⛔️ TypeError: 'dict' object is not callable print(emp.address())

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 rename the class method.

main.py
class Employee(): def __init__(self, address): # 👇️ this attribute hides the method self.address = address # 👇️ same name as class variable def get_address(self): return self.address emp = Employee({'country': 'Austria', 'city': 'Linz'}) # 👇️ {'country': 'Austria', 'city': 'Linz'} print(emp.get_address())

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

Use the search field on my Home Page to filter through my more than 3,000 articles.