Last updated: Apr 8, 2024
Reading time ยท 5 min
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'] .
Here is an example of how the error occurs.
Copied!my_dict = 'name': 'Bobby Hadz', 'age': 30> # โ๏ธ TypeError: 'dict' object is not callable print(my_dict('name')) # ๐๏ธ Uses parentheses instead of square brackets
The issue is that we used parentheses instead of square brackets when accessing a key in the dictionary.
Copied!my_dict = 'name': 'Bobby Hadz', 'age': 30> # โ Use square brackets print(my_dict['name']) # ๐๏ธ "Bobby Hadz" print(my_dict['age']) # ๐๏ธ 30
Overriding the built-in dict function also causes the error. Here is an example.
Copied!# ๐๏ธ overrides built-in dict function dict = 'name': 'Bobby Hadz', 'age': 30> # โ๏ธ TypeError: 'dict' object is not callable print(dict(name='Bobby Hadz', age=30))
We declared a dict variable and set it to a dictionary.
The dict variable shadows the built-in dict() function, so when we try to call the function, we actually call our own variable.
To solve the error, rename your variable and restart your script.
Copied!# โ Not overriding built-in dict anymore my_dict = 'name': 'Bobby Hadz', 'age': 30> print(dict(name='Bobby Hadz', age=30))
Now the variable name no longer clashes with the built-in dict() class.
The error occurs for multiple reasons:
Another common cause of the error is simply trying to call a dict as a function.
Copied!example = 'name': 'Bobby Hadz', 'age': 30> # โ๏ธ TypeError: 'dict' object is not callable example()
To solve the error, you either have to remove the parentheses or figure out how the variable got assigned a dictionary instead of a function or a class.
Copied!example = 'name': 'Bobby Hadz', 'age': 30> print(example) # ๐๏ธ
Removing the parentheses resolved the issue.
If the variable is supposed to store a function instead of a dict , track down where it got set to a dictionary and correct the assignment.
Make sure you don't have a function and a variable with the same name.
Copied!def example(): return 'bobbyhadz.com' # ๐๏ธ Shadowing the function example = 'name': 'Bobby Hadz', '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.
Copied!def example(): return 'bobbyhadz.com' # ๐๏ธ Shadowing the function my_dict = 'name': 'Bobby Hadz', 'age': 30> print(example()) # ๐๏ธ bobbyhadz.com
We renamed the variable and it no longer clashes with the function, so the issue is resolved.
The error is also caused when we have a class method and a class property with the same name.
Copied!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.
Copied!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'>) # ๐๏ธ print(emp.get_address())
Now the class property no longer shadows the method, so we can call the method without any issues.
Calling a function that returns a dictionary twice also causes the error.
Copied!def example(): return 'name': 'bobby hadz', 'age': 30> # โ๏ธ TypeError: 'dict' object is not callable example()()
Notice that we used two sets of parentheses.
The first set invokes the function and the function returns a dictionary.
The second set of parentheses calls the dictionary as a function and causes the error.
Remove the second set of parentheses to resolve the issue.
Copied!def example(): return 'name': 'bobby hadz', 'age': 30> print(example()) # ๐๏ธ
To solve the "TypeError: 'dict' object is not callable" error, make sure:
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.