blob: 1c756742cd5241a1968f2441c381f54062a4a039 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`UserDict` --- Class wrapper for dictionary objects
2========================================================
3
4.. module:: UserDict
5 :synopsis: Class wrapper for dictionary objects.
6
7
Éric Araujo29a0b572011-08-19 02:14:03 +02008**Source code:** :source:`Lib/UserDict.py`
9
10--------------
11
Georg Brandl8ec7f652007-08-15 14:28:01 +000012The module defines a mixin, :class:`DictMixin`, defining all dictionary methods
13for classes that already have a minimum mapping interface. This greatly
14simplifies writing classes that need to be substitutable for dictionaries (such
15as the shelve module).
16
Georg Brandl359b9e92008-02-21 20:33:38 +000017This module also defines a class, :class:`UserDict`, that acts as a wrapper
Georg Brandl8ec7f652007-08-15 14:28:01 +000018around dictionary objects. The need for this class has been largely supplanted
19by the ability to subclass directly from :class:`dict` (a feature that became
20available starting with Python version 2.2). Prior to the introduction of
21:class:`dict`, the :class:`UserDict` class was used to create dictionary-like
22sub-classes that obtained new behaviors by overriding existing methods or adding
23new ones.
24
25The :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
47In addition to supporting the methods and operations of mappings (see section
48:ref:`typesmapping`), :class:`UserDict` and :class:`IterableUserDict` instances
49provide 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 Hettinger7e336632008-02-08 23:57:06 +000074 Starting with Python version 2.6, it is recommended to use
75 :class:`collections.MutableMapping` instead of :class:`DictMixin`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000076
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
86 This module is available for backward compatibility only. If you are writing
87 code that does not need to work with versions of Python earlier than Python 2.2,
88 please consider subclassing directly from the built-in :class:`list` type.
89
90This module defines a class that acts as a wrapper around list objects. It is a
91useful base class for your own list-like classes, which can inherit from them
92and override existing methods or add new ones. In this way one can add new
93behaviors to lists.
94
95The :mod:`UserList` module defines the :class:`UserList` class:
96
97
98.. class:: UserList([list])
99
100 Class that simulates a list. The instance's contents are kept in a regular
101 list, which is accessible via the :attr:`data` attribute of :class:`UserList`
102 instances. The instance's contents are initially set to a copy of *list*,
103 defaulting to the empty list ``[]``. *list* can be any iterable, e.g. a
104 real Python list or a :class:`UserList` object.
105
Brett Cannon6983ff72008-05-29 21:28:55 +0000106 .. note::
107 The :class:`UserList` class has been moved to the :mod:`collections`
108 module in Python 3.0. The :term:`2to3` tool will automatically adapt
109 imports when converting your sources to 3.0.
110
111
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112In addition to supporting the methods and operations of mutable sequences (see
113section :ref:`typesseq`), :class:`UserList` instances provide the following
114attribute:
115
116
117.. attribute:: UserList.data
118
119 A real Python list object used to store the contents of the :class:`UserList`
120 class.
121
122**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
123offer a constructor which can be called with either no arguments or one
124argument. List operations which return a new sequence attempt to create an
125instance of the actual implementation class. To do so, it assumes that the
126constructor can be called with a single parameter, which is a sequence object
127used as a data source.
128
129If a derived class does not wish to comply with this requirement, all of the
130special methods supported by this class will need to be overridden; please
131consult the sources for information about the methods which need to be provided
132in that case.
133
134.. versionchanged:: 2.0
135 Python versions 1.5.2 and 1.6 also required that the constructor be callable
136 with no parameters, and offer a mutable :attr:`data` attribute. Earlier
137 versions of Python did not attempt to create instances of the derived class.
138
139
140:mod:`UserString` --- Class wrapper for string objects
141======================================================
142
143.. module:: UserString
144 :synopsis: Class wrapper for string objects.
145.. moduleauthor:: Peter Funk <pf@artcom-gmbh.de>
146.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
147
148
149.. note::
150
151 This :class:`UserString` class from this module is available for backward
152 compatibility only. If you are writing code that does not need to work with
153 versions of Python earlier than Python 2.2, please consider subclassing directly
154 from the built-in :class:`str` type instead of using :class:`UserString` (there
155 is no built-in equivalent to :class:`MutableString`).
156
157This module defines a class that acts as a wrapper around string objects. It is
158a useful base class for your own string-like classes, which can inherit from
159them and override existing methods or add new ones. In this way one can add new
160behaviors to strings.
161
162It should be noted that these classes are highly inefficient compared to real
163string or Unicode objects; this is especially the case for
164:class:`MutableString`.
165
166The :mod:`UserString` module defines the following classes:
167
168
169.. class:: UserString([sequence])
170
171 Class that simulates a string or a Unicode string object. The instance's
172 content is kept in a regular string or Unicode string object, which is
173 accessible via the :attr:`data` attribute of :class:`UserString` instances. The
174 instance's contents are initially set to a copy of *sequence*. *sequence* can
175 be either a regular Python string or Unicode string, an instance of
176 :class:`UserString` (or a subclass) or an arbitrary sequence which can be
177 converted into a string using the built-in :func:`str` function.
178
Brett Cannon6983ff72008-05-29 21:28:55 +0000179 .. note::
180 The :class:`UserString` class has been moved to the :mod:`collections`
181 module in Python 3.0. The :term:`2to3` tool will automatically adapt
182 imports when converting your sources to 3.0.
183
184
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
186.. class:: MutableString([sequence])
187
188 This class is derived from the :class:`UserString` above and redefines strings
189 to be *mutable*. Mutable strings can't be used as dictionary keys, because
190 dictionaries require *immutable* objects as keys. The main intention of this
191 class is to serve as an educational example for inheritance and necessity to
192 remove (override) the :meth:`__hash__` method in order to trap attempts to use a
193 mutable object as dictionary key, which would be otherwise very error prone and
194 hard to track down.
195
Brett Cannonabb34fe2008-05-29 05:08:50 +0000196 .. deprecated:: 2.6
197 The :class:`MutableString` class has been removed in Python 3.0.
198
Georg Brandl8ec7f652007-08-15 14:28:01 +0000199In addition to supporting the methods and operations of string and Unicode
200objects (see section :ref:`string-methods`), :class:`UserString` instances
201provide the following attribute:
202
203
204.. attribute:: MutableString.data
205
206 A real Python string or Unicode object used to store the content of the
207 :class:`UserString` class.
208