This document is for an old version of Python that is no longer supported. You should upgrade and read the Python 当前稳定版本的文档.

# 8.12. `UserDict` — Class wrapper for dictionary objects

Source code: Lib/UserDict.py

The module defines a mixin, `DictMixin`, defining all dictionary methods for classes that already have a minimum mapping interface. This greatly simplifies writing classes that need to be substitutable for dictionaries (such as the shelve module).

This module also defines a class, `UserDict`, that acts as a wrapper around dictionary objects. The need for this class has been largely supplanted by the ability to subclass directly from `dict` (a feature that became available starting with Python version 2.2). Prior to the introduction of `dict`, the `UserDict` class was used to create dictionary-like sub-classes that obtained new behaviors by overriding existing methods or adding new ones.

The `UserDict` module defines the `UserDict` class and `DictMixin`:

class `UserDict.``UserDict`([initialdata])

  Class that simulates a dictionary. The instance’s contents are kept in a regular dictionary, which is accessible via the `data` attribute of `UserDict` instances. If initialdata is provided, `data` is initialized with its contents; note that a reference to initialdata will not be kept, allowing it be used for other purposes.

  注解

  For backward compatibility, instances of `UserDict` are not iterable.

class `UserDict.``IterableUserDict`([initialdata])

  Subclass of `UserDict` that supports direct iteration (e.g. `for key in myDict`).

In addition to supporting the methods and operations of mappings (see section 映射类型 — dict), `UserDict` and `IterableUserDict` instances provide the following attribute:

`IterableUserDict.``data`

  A real dictionary used to store the contents of the `UserDict` class.

class `UserDict.``DictMixin`

  Mixin defining all dictionary methods for classes that already have a minimum dictionary interface including `__getitem__()`, `__setitem__()`, `__delitem__()`, and `keys()`.

  This mixin should be used as a superclass. Adding each of the above methods adds progressively more functionality. For instance, defining all but `__delitem__()` will preclude only `pop()` and `popitem()` from the full interface.

  In addition to the four base methods, progressively more efficiency comes with defining `__contains__()`, `__iter__()`, and `iteritems()`.

  Since the mixin has no knowledge of the subclass constructor, it does not define `__init__()` or `copy()`.

  Starting with Python version 2.6, it is recommended to use `collections.MutableMapping` instead of `DictMixin`.

  Note that DictMixin does not implement the `viewkeys()`, `viewvalues()`, or `viewitems()` methods.

# 8.13. `UserList` — Class wrapper for list objects

注解

When Python 2.2 was released, many of the use cases for this class were subsumed by the ability to subclass `list` directly. However, a handful of use cases remain.

This module provides a list-interface around an underlying data store. By default, that data store is a `list`; however, it can be used to wrap a list-like interface around other objects (such as persistent storage).

In addition, this class can be mixed-in with built-in classes using multiple inheritance. This can sometimes be useful. For example, you can inherit from `UserList` and `str` at the same time. That would not be possible with both a real `list` and a real `str`.

This module defines a class that acts as a wrapper around list objects. It is a useful base class for your own list-like classes, which can inherit from them and override existing methods or add new ones. In this way one can add new behaviors to lists.

The `UserList` module defines the `UserList` class:

class `UserList.``UserList`([list])

  Class that simulates a list. The instance’s contents are kept in a regular list, which is accessible via the `data` attribute of `UserList` instances. The instance’s contents are initially set to a copy of list, defaulting to the empty list `[]`. list can be any iterable, e.g. a real Python list or a `UserList` object.

  注解

  The `UserList` class has been moved to the `collections` module in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

In addition to supporting the methods and operations of mutable sequences (see section Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange), `UserList` instances provide the following attribute:

`UserList.``data`

  A real Python list object used to store the contents of the `UserList` class.

Subclassing requirements: Subclasses of `UserList` are expected to offer a constructor which can be called with either no arguments or one argument. List operations which return a new sequence attempt to create an instance of the actual implementation class. To do so, it assumes that the constructor can be called with a single parameter, which is a sequence object used as a data source.

If a derived class does not wish to comply with this requirement, all of the special methods supported by this class will need to be overridden; please consult the sources for information about the methods which need to be provided in that case.

在 2.0 版更改: Python versions 1.5.2 and 1.6 also required that the constructor be callable with no parameters, and offer a mutable `data` attribute. Earlier versions of Python did not attempt to create instances of the derived class.

# 8.14. `UserString` — Class wrapper for string objects

注解

This `UserString` class from this module is available for backward compatibility only. If you are writing code that does not need to work with versions of Python earlier than Python 2.2, please consider subclassing directly from the built-in `str` type instead of using `UserString` (there is no built-in equivalent to `MutableString`).

This module defines a class that acts as a wrapper around string objects. It is a useful base class for your own string-like classes, which can inherit from them and override existing methods or add new ones. In this way one can add new behaviors to strings.

It should be noted that these classes are highly inefficient compared to real string or Unicode objects; this is especially the case for `MutableString`.

The `UserString` module defines the following classes:

class `UserString.``UserString`([sequence])

  Class that simulates a string or a Unicode string object. The instance’s content is kept in a regular string or Unicode string object, which is accessible via the `data` attribute of `UserString` instances. The instance’s contents are initially set to a copy of sequence. sequence can be either a regular Python string or Unicode string, an instance of `UserString` (or a subclass) or an arbitrary sequence which can be converted into a string using the built-in `str()` function.

  注解

  The `UserString` class has been moved to the `collections` module in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

class `UserString.``MutableString`([sequence])

  This class is derived from the `UserString` above and redefines strings to be mutable. Mutable strings can’t be used as dictionary keys, because dictionaries require immutable objects as keys. The main intention of this class is to serve as an educational example for inheritance and necessity to remove (override) the `__hash__()` method in order to trap attempts to use a mutable object as dictionary key, which would be otherwise very error prone and hard to track down.

  2.6 版后已移除: The `MutableString` class has been removed in Python 3.

In addition to supporting the methods and operations of string and Unicode objects (see section 字符串的方法), `UserString` instances provide the following attribute:

`MutableString.``data`

  A real Python string or Unicode object used to store the content of the `UserString` class.