Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`UserDict` --- Class wrapper for dictionary objects |
| 2 | ======================================================== |
| 3 | |
| 4 | .. module:: UserDict |
| 5 | :synopsis: Class wrapper for dictionary objects. |
| 6 | |
| 7 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 8 | **Source code:** :source:`Lib/UserDict.py` |
| 9 | |
| 10 | -------------- |
| 11 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 12 | The module defines a mixin, :class:`DictMixin`, defining all dictionary methods |
| 13 | for classes that already have a minimum mapping interface. This greatly |
| 14 | simplifies writing classes that need to be substitutable for dictionaries (such |
| 15 | as the shelve module). |
| 16 | |
Georg Brandl | 359b9e9 | 2008-02-21 20:33:38 +0000 | [diff] [blame] | 17 | This module also defines a class, :class:`UserDict`, that acts as a wrapper |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | around dictionary objects. The need for this class has been largely supplanted |
| 19 | by the ability to subclass directly from :class:`dict` (a feature that became |
| 20 | available starting with Python version 2.2). Prior to the introduction of |
| 21 | :class:`dict`, the :class:`UserDict` class was used to create dictionary-like |
| 22 | sub-classes that obtained new behaviors by overriding existing methods or adding |
| 23 | new ones. |
| 24 | |
| 25 | The :mod:`UserDict` module defines the :class:`UserDict` class and |
| 26 | :class:`DictMixin`: |
| 27 | |
| 28 | |
| 29 | .. class:: UserDict([initialdata]) |
| 30 | |
| 31 | Class that simulates a dictionary. The instance's contents are kept in a |
| 32 | regular dictionary, which is accessible via the :attr:`data` attribute of |
| 33 | :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is |
| 34 | initialized with its contents; note that a reference to *initialdata* will not |
| 35 | be kept, allowing it be used for other purposes. |
| 36 | |
| 37 | .. note:: |
| 38 | |
| 39 | For backward compatibility, instances of :class:`UserDict` are not iterable. |
| 40 | |
| 41 | |
| 42 | .. class:: IterableUserDict([initialdata]) |
| 43 | |
| 44 | Subclass of :class:`UserDict` that supports direct iteration (e.g. ``for key in |
| 45 | myDict``). |
| 46 | |
| 47 | In addition to supporting the methods and operations of mappings (see section |
| 48 | :ref:`typesmapping`), :class:`UserDict` and :class:`IterableUserDict` instances |
| 49 | provide the following attribute: |
| 50 | |
| 51 | |
| 52 | .. attribute:: IterableUserDict.data |
| 53 | |
| 54 | A real dictionary used to store the contents of the :class:`UserDict` class. |
| 55 | |
| 56 | |
| 57 | .. class:: DictMixin() |
| 58 | |
| 59 | Mixin defining all dictionary methods for classes that already have a minimum |
| 60 | dictionary interface including :meth:`__getitem__`, :meth:`__setitem__`, |
| 61 | :meth:`__delitem__`, and :meth:`keys`. |
| 62 | |
| 63 | This mixin should be used as a superclass. Adding each of the above methods |
| 64 | adds progressively more functionality. For instance, defining all but |
| 65 | :meth:`__delitem__` will preclude only :meth:`pop` and :meth:`popitem` from the |
| 66 | full interface. |
| 67 | |
| 68 | In addition to the four base methods, progressively more efficiency comes with |
| 69 | defining :meth:`__contains__`, :meth:`__iter__`, and :meth:`iteritems`. |
| 70 | |
| 71 | Since the mixin has no knowledge of the subclass constructor, it does not define |
| 72 | :meth:`__init__` or :meth:`copy`. |
| 73 | |
Raymond Hettinger | 7e33663 | 2008-02-08 23:57:06 +0000 | [diff] [blame] | 74 | Starting with Python version 2.6, it is recommended to use |
| 75 | :class:`collections.MutableMapping` instead of :class:`DictMixin`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 76 | |
| 77 | :mod:`UserList` --- Class wrapper for list objects |
| 78 | ================================================== |
| 79 | |
| 80 | .. module:: UserList |
| 81 | :synopsis: Class wrapper for list objects. |
| 82 | |
| 83 | |
| 84 | .. note:: |
| 85 | |
Raymond Hettinger | c5f310c | 2012-02-01 19:00:09 -0800 | [diff] [blame] | 86 | When Python 2.2 was released, many of the use cases for this class were |
| 87 | subsumed by the ability to subclass :class:`list` directly. However, a |
| 88 | handful of use cases remain. |
| 89 | |
| 90 | This module provides a list-interface around an underlying data store. By |
| 91 | default, that data store is a :class:`list`; however, it can be used to wrap |
| 92 | a list-like interface around other objects (such as persistent storage). |
| 93 | |
| 94 | In addition, this class can be mixed-in with built-in classes using multiple |
| 95 | inheritance. This can sometimes be useful. For example, you can inherit |
| 96 | from :class:`UserList` and :class:`str` at the same time. That would not be |
| 97 | possible with both a real :class:`list` and a real :class:`str`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 98 | |
| 99 | This module defines a class that acts as a wrapper around list objects. It is a |
| 100 | useful base class for your own list-like classes, which can inherit from them |
| 101 | and override existing methods or add new ones. In this way one can add new |
| 102 | behaviors to lists. |
| 103 | |
| 104 | The :mod:`UserList` module defines the :class:`UserList` class: |
| 105 | |
| 106 | |
| 107 | .. class:: UserList([list]) |
| 108 | |
| 109 | Class that simulates a list. The instance's contents are kept in a regular |
| 110 | list, which is accessible via the :attr:`data` attribute of :class:`UserList` |
| 111 | instances. The instance's contents are initially set to a copy of *list*, |
| 112 | defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a |
| 113 | real Python list or a :class:`UserList` object. |
| 114 | |
Brett Cannon | 6983ff7 | 2008-05-29 21:28:55 +0000 | [diff] [blame] | 115 | .. note:: |
| 116 | The :class:`UserList` class has been moved to the :mod:`collections` |
| 117 | module in Python 3.0. The :term:`2to3` tool will automatically adapt |
| 118 | imports when converting your sources to 3.0. |
| 119 | |
| 120 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 121 | In addition to supporting the methods and operations of mutable sequences (see |
| 122 | section :ref:`typesseq`), :class:`UserList` instances provide the following |
| 123 | attribute: |
| 124 | |
| 125 | |
| 126 | .. attribute:: UserList.data |
| 127 | |
| 128 | A real Python list object used to store the contents of the :class:`UserList` |
| 129 | class. |
| 130 | |
| 131 | **Subclassing requirements:** Subclasses of :class:`UserList` are expect to |
| 132 | offer a constructor which can be called with either no arguments or one |
| 133 | argument. List operations which return a new sequence attempt to create an |
| 134 | instance of the actual implementation class. To do so, it assumes that the |
| 135 | constructor can be called with a single parameter, which is a sequence object |
| 136 | used as a data source. |
| 137 | |
| 138 | If a derived class does not wish to comply with this requirement, all of the |
| 139 | special methods supported by this class will need to be overridden; please |
| 140 | consult the sources for information about the methods which need to be provided |
| 141 | in that case. |
| 142 | |
| 143 | .. versionchanged:: 2.0 |
| 144 | Python versions 1.5.2 and 1.6 also required that the constructor be callable |
| 145 | with no parameters, and offer a mutable :attr:`data` attribute. Earlier |
| 146 | versions of Python did not attempt to create instances of the derived class. |
| 147 | |
| 148 | |
| 149 | :mod:`UserString` --- Class wrapper for string objects |
| 150 | ====================================================== |
| 151 | |
| 152 | .. module:: UserString |
| 153 | :synopsis: Class wrapper for string objects. |
| 154 | .. moduleauthor:: Peter Funk <pf@artcom-gmbh.de> |
| 155 | .. sectionauthor:: Peter Funk <pf@artcom-gmbh.de> |
| 156 | |
| 157 | |
| 158 | .. note:: |
| 159 | |
| 160 | This :class:`UserString` class from this module is available for backward |
| 161 | compatibility only. If you are writing code that does not need to work with |
| 162 | versions of Python earlier than Python 2.2, please consider subclassing directly |
| 163 | from the built-in :class:`str` type instead of using :class:`UserString` (there |
| 164 | is no built-in equivalent to :class:`MutableString`). |
| 165 | |
| 166 | This module defines a class that acts as a wrapper around string objects. It is |
| 167 | a useful base class for your own string-like classes, which can inherit from |
| 168 | them and override existing methods or add new ones. In this way one can add new |
| 169 | behaviors to strings. |
| 170 | |
| 171 | It should be noted that these classes are highly inefficient compared to real |
| 172 | string or Unicode objects; this is especially the case for |
| 173 | :class:`MutableString`. |
| 174 | |
| 175 | The :mod:`UserString` module defines the following classes: |
| 176 | |
| 177 | |
| 178 | .. class:: UserString([sequence]) |
| 179 | |
| 180 | Class that simulates a string or a Unicode string object. The instance's |
| 181 | content is kept in a regular string or Unicode string object, which is |
| 182 | accessible via the :attr:`data` attribute of :class:`UserString` instances. The |
| 183 | instance's contents are initially set to a copy of *sequence*. *sequence* can |
| 184 | be either a regular Python string or Unicode string, an instance of |
| 185 | :class:`UserString` (or a subclass) or an arbitrary sequence which can be |
| 186 | converted into a string using the built-in :func:`str` function. |
| 187 | |
Brett Cannon | 6983ff7 | 2008-05-29 21:28:55 +0000 | [diff] [blame] | 188 | .. note:: |
| 189 | The :class:`UserString` class has been moved to the :mod:`collections` |
| 190 | module in Python 3.0. The :term:`2to3` tool will automatically adapt |
| 191 | imports when converting your sources to 3.0. |
| 192 | |
| 193 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 194 | |
| 195 | .. class:: MutableString([sequence]) |
| 196 | |
| 197 | This class is derived from the :class:`UserString` above and redefines strings |
| 198 | to be *mutable*. Mutable strings can't be used as dictionary keys, because |
| 199 | dictionaries require *immutable* objects as keys. The main intention of this |
| 200 | class is to serve as an educational example for inheritance and necessity to |
| 201 | remove (override) the :meth:`__hash__` method in order to trap attempts to use a |
| 202 | mutable object as dictionary key, which would be otherwise very error prone and |
| 203 | hard to track down. |
| 204 | |
Brett Cannon | abb34fe | 2008-05-29 05:08:50 +0000 | [diff] [blame] | 205 | .. deprecated:: 2.6 |
| 206 | The :class:`MutableString` class has been removed in Python 3.0. |
| 207 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 208 | In addition to supporting the methods and operations of string and Unicode |
| 209 | objects (see section :ref:`string-methods`), :class:`UserString` instances |
| 210 | provide the following attribute: |
| 211 | |
| 212 | |
| 213 | .. attribute:: MutableString.data |
| 214 | |
| 215 | A real Python string or Unicode object used to store the content of the |
| 216 | :class:`UserString` class. |
| 217 | |