Custom Python Dictionaries: Inheriting From dict vs UserDict
Creating dictionary-like classes may be a requirement in your Python career. Specifically, you may be interested in making custom dictionaries with modified behavior, new functionalities, or both. In Python, you can do this by inheriting from an abstract base class, by subclassing the built-in dict
class directly, or by inheriting from UserDict
.
In this tutorial, you’ll learn how to:
- Create dictionary-like classes by inheriting from the built-in
dict
class - Identify common pitfalls that can happen when inheriting from
dict
- Build dictionary-like classes by subclassing
UserDict
from thecollections
module
Additionally, you’ll code a few examples that’ll help you understand the pros and cons of using dict
vs UserDict
to create your custom dictionary classes.
To get the