TypeError: 'dict' object is not callable in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# 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 an example of how the error occurs.

main.py
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.

main.py
my_dict = {'name': 'Bobby Hadz', 'age': 30} # โœ… Use square brackets print(my_dict['name']) # ๐Ÿ‘‰๏ธ "Bobby Hadz" print(my_dict['age']) # ๐Ÿ‘‰๏ธ 30

use square brackets to access dictionary key

# Overriding the built-in dict() function by mistake

Overriding the built-in dict function also causes the error. Here is an example.

main.py
# ๐Ÿ‘‡๏ธ overrides built-in dict function dict = {'name': 'Bobby Hadz', 'age': 30} # โ›”๏ธ TypeError: 'dict' object is not callable print(dict(name='Bobby Hadz', age=30))

overriding built in dict function

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.

main.py
# โœ… 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.

# Common causes of the error

The error occurs for multiple reasons:

  • Trying to access a dictionary key with parentheses () instead of square brackets [].
  • Having a function and a variable with the same name.
  • Overriding a built-in function by mistake and setting it to a dictionary.
  • Having a class method and a class property with the same name.
  • Calling a function that returns a dictionary twice.

# Trying to call a dict object as a function

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

main.py
example = {'name': 'Bobby Hadz', 'age': 30} # โ›”๏ธ TypeError: 'dict' object is not callable example()

trying to call dict object as function

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.

main.py
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.

# Having a function and a variable with the same name

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

main.py
def example(): return 'bobbyhadz.com' # ๐Ÿ‘‡๏ธ Shadowing the function example = {'name': 'Bobby Hadz', 'age': 30} # โ›”๏ธ TypeError: 'dict' object is not callable example()

having function and variable with same name

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.

main.py
def example(): return 'bobbyhadz.com' # ๐Ÿ‘‡๏ธ Shadowing the function my_dict = {'name': 'Bobby Hadz', 'age': 30} print(example()) # ๐Ÿ‘‰๏ธ bobbyhadz.com

renaming the variable or the function

We renamed the variable and it no longer clashes with the function, so the issue is resolved.

# Having a class method and a class property with the same name

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())

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

Calling a function that returns a dictionary twice also causes the error.

main.py
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.

main.py
def example(): return {'name': 'bobby hadz', 'age': 30} print(example()) # ๐Ÿ‘‰๏ธ {'name': 'bobby hadz', 'age': 30}

# Conclusion

To solve the "TypeError: 'dict' object is not callable" error, make sure:

  • You aren't trying to access a dictionary key with parentheses () instead of square brackets [].
  • You don't have a function and a variable with the same name.
  • You aren't overriding a built-in function and setting it to a dictionary.
  • You don't have a class method and a property with the same name.
  • You aren't calling a function that returns a dictionary twice.

# Additional Resources

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.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev