Last modified: Dec 06, 2025 By Alexander Williams
Fix TypeError: '__init__' requires 'dict' object
Python errors can be confusing. One such error is the TypeError about __init__ and a 'dict' object. This error often stops beginners.
It usually happens when mixing up class inheritance. Specifically, it involves the built-in dict type. Understanding this is key to fixing it.
This article will explain the error in simple terms. We will show common causes and provide clear solutions. You will learn how to avoid this mistake in your code.
Understanding the Error Message
The full error looks like this: "TypeError: descriptor '__init__' requires a 'dict' object but received a 'YourClassName'". This is a Python runtime error.
It tells you that Python tried to call the __init__ method of the dict class. But it received an object of a different type instead.
This is almost always related to incorrect class definition. The error points to a misunderstanding of how to properly subclass the built-in dict type.
Common Cause: Incorrect dict Subclassing
The primary cause is a mistake in inheriting from the dict class. In Python 3, the dict class is implemented in C.
Its __init__ method is a special descriptor. It expects to be called on a new dict instance. If your class setup is wrong, this fails.
Here is a typical example of code that will produce this error.
# Incorrect way to subclass dict
class MyDict(dict):
def __init__(self, data, extra_info):
self.extra_info = extra_info
# Missing call to super().__init__()
# This causes the error
# Trying to create an instance
obj = MyDict({'a': 1}, "my info")
Traceback (most recent call last):
File "script.py", line 8, in
obj = MyDict({'a': 1}, "my info")
TypeError: descriptor '__init__' requires a 'dict' object but received a 'MyDict'
The error occurs because the child class's __init__ overrides the parent's. It does not properly initialize the parent dict part of the object.
Python's internal machinery gets confused. It tries to call dict.__init__ with the wrong context.
The Correct Solution: Using super().__init__()
The fix is to ensure the parent class's __init__ method is called correctly. You must use the super() function.
This function returns a temporary object of the superclass. It lets you call its methods. This is crucial for proper initialization.
Here is the corrected version of the previous example.
# Correct way to subclass dict
class MyDict(dict):
def __init__(self, data, extra_info):
# First, initialize the dict part with the data
super().__init__(data)
# Then, set the custom attribute
self.extra_info = extra_info
# Now it works correctly
obj = MyDict({'a': 1}, "my info")
print(obj) # Outputs: {'a': 1}
print(obj.extra_info) # Outputs: my info
{'a': 1}
my info
The key is the line super().__init__(data). This line properly passes the initial dictionary data to the parent dict.__init__ method.
After that, you can safely assign your own instance attributes. The object is now a fully functional dictionary with extra features.
Alternative: Using collections.UserDict
If you find subclassing dict tricky, consider collections.UserDict. It's designed for easier inheritance.
UserDict is a wrapper around a real dictionary. It's implemented in Python, not C. This makes its behavior more predictable for subclassing.
Here is how you would use UserDict to achieve the same goal.
from collections import UserDict
class MyUserDict(UserDict):
def __init__(self, data, extra_info):
super().__init__(data) # Initialize the UserDict
self.extra_info = extra_info
obj = MyUserDict({'b': 2}, "extra data")
print(obj.data) # The actual dict is in .data
print(obj.extra_info)
{'b': 2}
extra data
Using UserDict often avoids the descriptor error entirely. It's a more forgiving class for creating custom dictionary-like objects.
This is a great solution for beginners or when you need simple customization. Remember to access the data via the .data attribute.
Related Python TypeErrors
Understanding one TypeError helps with others. Python has many similar descriptor and object errors.
For instance, you might encounter a Fix TypeError: 'module' object is not callable. This happens when you try to call a module as a function.
Another common issue is the Fix TypeError: expected string or bytes-like object. This occurs with functions like re.match() that need string input.
Also, watch for the Fix TypeError: 'method' object is not subscriptable. This error appears when you use square brackets on a method.
Best Practices to Avoid the Error
Always call super().__init__() in your subclass __init__ method. Do this when inheriting from built-in types like dict, list, or str.
Call it at the beginning of your method. This ensures the parent object is set up first. Then you can add your custom logic safely.
If you are new to inheritance, practice with simple classes first. Move to built-in types once you are comfortable with super().
Read the error traceback carefully. It tells you the exact line number. Look at the class names involved to diagnose the issue.
Conclusion
The "descriptor '__init__' requires a 'dict' object" error is a classic Python pitfall. It stems from improper initialization in a subclass.
The fix is straightforward. You must correctly call the parent class's initializer using super().__init__(). Pass any necessary data to it.
For simpler cases, the collections.UserDict class is an excellent alternative. It provides a more intuitive way to create custom dictionaries.
Remember, understanding inheritance is a core Python skill. Mastering it will help you fix this and many other errors. Keep your code clean and your initializers correct.