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 theUserDict
class
When a UserDict
is created, it accepts an optional argument of the initial data it is to hold; this initial data is accessible by the data
attribute.