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.
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.
my_dict = {'name': 'Bobby Hadz', 'age': 30} # โ Use square brackets print(my_dict['name']) # ๐๏ธ "Bobby Hadz" print(my_dict['age']) # ๐๏ธ 30
dict()
function by mistakeOverriding the built-in dict
function also causes the error. Here is an
example.
# ๐๏ธ 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.
# โ 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:
()
instead of square
brackets []
.dict
object as a functionAnother common cause of the error is simply trying to call a dict
as a
function.
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.
example = {'name': 'Bobby Hadz', 'age': 30} print(example) # ๐๏ธ {'name': 'Bobby Hadz', 'age': 30}
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.
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.
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.
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.
To solve the error, you have to rename the class method.
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())
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.
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.
def example(): return {'name': 'bobby hadz', 'age': 30} print(example()) # ๐๏ธ {'name': 'bobby hadz', 'age': 30}
To solve the "TypeError: 'dict' object is not callable" error, make sure:
()
instead of
square brackets []
.You can learn more about the related topics by checking out the following tutorials: