Skip to main content

How to Resolve Python "TypeError: 'dict' object is not callable"

The TypeError: 'dict' object is not callable is a frequent error in Python that occurs when you mistakenly try to use a dictionary object as if it were a function, typically by appending parentheses () to it. Dictionaries are data structures for key-value storage, not executable code blocks.

This guide explains the common scenarios leading to this error and provides clear solutions to fix it.

Understanding the Error: Callable vs. Non-Callable Objects

In Python, objects that can be "called" using parentheses () are typically functions, methods, or class constructors. This act of calling executes some code. Data structures like dictionaries (dict), lists (list), strings (str), integers (int), etc., are not callable by default; they store data but aren't executable functions themselves. Attempting to call a non-callable object like a dictionary results in a TypeError.

Cause 1: Using () Instead of [] for Key Access (Most Common)

This is the most frequent accidental cause. When trying to retrieve a value associated with a key from a dictionary, you must use square brackets [], not parentheses ().

my_info = {'city': 'London', 'country': 'UK'}

# Error Scenario
try:
# ⛔️ TypeError: 'dict' object is not callable
# Incorrectly using parentheses () for key access
city = my_info('city')
print(city)
except TypeError as e:
print(e)

# Solution: Use Square Brackets []
# ✅ Correctly use square brackets [] for key access
city = my_info['city']
country = my_info['country']

print(f"City: {city}") # Output: City: London
print(f"Country: {country}") # Output: Country: UK

Remember: my_dict['key'] accesses the value, my_dict('key') tries to call the dictionary.

Cause 2: Accidentally Calling a Dictionary Variable

Sometimes, you might simply have a variable holding a dictionary and mistakenly add parentheses to it later, perhaps intending to print it or pass it somewhere.

user_data = {'id': 101, 'status': 'active'}

# Error Scenario
try:
# ⛔️ TypeError: 'dict' object is not callable
# Calling the dictionary variable itself
user_data()
except TypeError as e:
print(e)

# Solution: Remove the parentheses if calling was unintended
# ✅ Just using the variable (e.g., printing it) is fine
print(user_data) # Output: {'id': 101, 'status': 'active'}
note

If you intended to call a function that uses the dict, make sure the variable name doesn't clash (see Cause 3).

Cause 3: Variable Name Clashing with Function Name

If you define a function and later assign a dictionary to a variable with the exact same name, the variable assignment hides the function. Trying to call the original function name will now attempt to call the dictionary stored in the variable.

def get_config():
"""Returns configuration settings."""
print("Original get_config function running...")
return {'theme': 'dark', 'font_size': 12}

# Function works initially
config = get_config()
print(f"Initial config: {config}")

# --- Later in the code ---
# ⚠️ Variable assigned the same name as the function!
get_config = {'theme': 'light', 'font_size': 10} # Now name refers to this dict

# Error Scenario
try:
# ⛔️ TypeError: 'dict' object is not callable
# This tries to call the dictionary {'theme': 'light', ...}
new_config = get_config()
print(new_config)
except TypeError as e:
print(f"Error: {e}")

# Solution: Rename the variable or the function
def get_config_func(): # Renamed function
print("Original get_config_func running...")
return {'theme': 'dark', 'font_size': 2}

current_config = {'theme': 'light', 'font_size': 10} # Use a different variable name

# ✅ Calling the renamed function works
new_config_fixed = get_config_func()
print(f"Fixed call result: {new_config_fixed}")
note

Use distinct names for functions and variables.

Cause 4: Shadowing the Built-in dict Type

Assigning a dictionary to a variable named exactly dict overrides Python's built-in dict() type constructor. Subsequent attempts to use the dict() constructor will erroneously try to call your dictionary variable.

# Error Scenario
# ⚠️ Assigning a dictionary to the name 'dict', hiding the built-in type
dict = {'a': 1, 'b': 2}
print(f"The variable 'dict' now holds: {dict}")

try:
# ⛔️ TypeError: 'dict' object is not callable
# This tries to call the variable dict = {'a': 1, 'b': 2}
new_dictionary = dict(c=3, d=4)
print(new_dictionary)
except TypeError as e:
print(e)

# Solution: Use a different variable name
# ✅ Avoid using built-in names for variables
my_own_dictionary = {'a': 1, 'b': 2}

# ✅ Now dict() refers to the built-in type constructor again
new_dictionary_fixed = dict(c=3, d=4) # Creates a new dictionary {'c': 3, 'd': 4}
print(f"Using built-in dict(): {new_dictionary_fixed}")
note

Never use built-in type or function names (like dict, list, str, int, sum, max, etc.) as variable names.

If you did this in an interactive session, restart it.

Cause 5: Class Attribute Name Clashing with Method Name

In a class definition, if an instance attribute and a method share the same name, accessing that name on an instance usually retrieves the attribute, shadowing the method. If that attribute holds a dictionary, trying to call the method name results in this TypeError.

class ConfigManager:
def __init__(self, settings_dict):
# Instance attribute named 'settings'
self.settings = settings_dict # Holds a dictionary

# Method with the *same name* as the attribute
def settings(self):
print("Method 'settings' called!") # This is hard to reach
return "Settings Method Result"

# Error Scenario
manager = ConfigManager({'user': 'guest', 'level': 1})
print(f"Attribute value: {manager.settings}") # Accesses the dict attribute

try:
# ⛔️ TypeError: 'dict' object is not callable
# manager.settings resolves to the dict attribute. Calling it fails.
result = manager.settings()
print(result)
except TypeError as e:
print(f"Error: {e}")

# Solution: Rename the method or attribute
class ConfigManagerFixed:
def __init__(self, settings_data):
# Use a different attribute name
self.settings_data = settings_data

# Or rename the method
def get_settings_info(self):
print("Method 'get_settings_info' called!")
return self.settings_data

manager_fixed = ConfigManagerFixed({'user': 'admin', 'level': 5})

# ✅ Call the distinct method name
result_fixed = manager_fixed.get_settings_info()
print(f"Fixed call result: {result_fixed}")

Maintain distinct names for attributes and methods within classes.

Cause 6: Calling a Function That Returns a Dictionary Twice

If a function returns a dictionary, calling it once yields the dictionary object. Adding a second set of parentheses attempts to call that returned dictionary.

def load_user_data():
print("Loading user data...")
return {'id': 'xyz', 'prefs': {'theme': 'dark'}}

# Error Scenario
try:
# ⛔️ TypeError: 'dict' object is not callable
# load_user_data() returns a dict. The second () tries to call that dict.
data = load_user_data()()
print(data)
except TypeError as e:
print(f"Error: {e}")

# Solution: Call the function only once
# ✅ Get the dictionary by calling the function once
data = load_user_data()
print(f"Data loaded: {data}") # Output: {'id': 'xyz', 'prefs': {'theme': 'dark'}}

Remove the extraneous parentheses.

Debugging Tip: Check Variable Types

When faced with this error, immediately check the type of the variable you're trying to call just before the error line:

variable_causing_error = ... # Whatever holds the dict

print(f"Type just before call: {type(variable_causing_error)}")
# Attempt the call that fails
try:
variable_causing_error()
except TypeError as e:
print(f"Error confirms it's not callable: {e}")

Seeing <class 'dict'> confirms the variable holds a dictionary, prompting you to investigate why you're trying to call it.

Conclusion

The TypeError: 'dict' object is not callable error arises from treating a Python dictionary like a function by using call parentheses (). Key solutions include:

  1. Using square brackets [] for dictionary key access (e.g., my_dict['key']).
  2. Removing accidental call parentheses () from dictionary variables.
  3. Using distinct names for variables and functions/methods/types to avoid shadowing.
  4. Calling functions that return dictionaries only once.

By identifying which of these common mistakes occurred, you can effectively resolve this TypeError.