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