Implementing UserDict
UserDict is a wrapper for dictionaries that makes it easier to subclass the dict class. It has been largely replaced by the ability to subclass dict directly, but it does make it easier to work with as it allows the underlying dictionary to be accessible as an attribute. Its primary use is for backwards-compatibility, that is, versions older then Python 2.2, so if you don't need the compatibility, it is generally better to just subclass dict.
The only special thing the UserDict has beyond the normal dictionary operations is a single attribute:
-
data: A real dictionary to hold the contents of the UserDict class
When a UserDict is created, it accepts an optional argument of the initial data it is to hold; this initial ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access