blob: 0585bda4a54f5aa5fc93826da9ea8ef2ec07681f [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
Raymond Hettingerc5f310c2012-02-01 19:00:09 -080086 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 Brandl8ec7f652007-08-15 14:28:01 +000098
99This module defines a class that acts as a wrapper around list objects. It is a
100useful base class for your own list-like classes, which can inherit from them
101and override existing methods or add new ones. In this way one can add new
102behaviors to lists.
103
104The :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 Cannon6983ff72008-05-29 21:28:55 +0000115 .. note::
116 The :class:`UserList` class has been moved to the :mod:`collections`
Ezio Melotti510ff542012-05-03 19:21:40 +0300117 module in Python 3. The :term:`2to3` tool will automatically adapt
118 imports when converting your sources to Python 3.
Brett Cannon6983ff72008-05-29 21:28:55 +0000119
120
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121In addition to supporting the methods and operations of mutable sequences (see
122section :ref:`typesseq`), :class:`UserList` instances provide the following
123attribute:
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
Zachary Wareaec30652014-01-13 20:38:17 -0600131**Subclassing requirements:** Subclasses of :class:`UserList` are expected to
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132offer a constructor which can be called with either no arguments or one
133argument. List operations which return a new sequence attempt to create an
134instance of the actual implementation class. To do so, it assumes that the
135constructor can be called with a single parameter, which is a sequence object
136used as a data source.
137
138If a derived class does not wish to comply with this requirement, all of the
139special methods supported by this class will need to be overridden; please
140consult the sources for information about the methods which need to be provided
141in 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
166This module defines a class that acts as a wrapper around string objects. It is
167a useful base class for your own string-like classes, which can inherit from
168them and override existing methods or add new ones. In this way one can add new
169behaviors to strings.
170
171It should be noted that these classes are highly inefficient compared to real
172string or Unicode objects; this is especially the case for
173:class:`MutableString`.
174
175The :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 Cannon6983ff72008-05-29 21:28:55 +0000188 .. note::
189 The :class:`UserString` class has been moved to the :mod:`collections`
Ezio Melotti510ff542012-05-03 19:21:40 +0300190 module in Python 3. The :term:`2to3` tool will automatically adapt
191 imports when converting your sources to Python 3.
Brett Cannon6983ff72008-05-29 21:28:55 +0000192
193
Georg Brandl8ec7f652007-08-15 14:28:01 +0000194
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 Cannonabb34fe2008-05-29 05:08:50 +0000205 .. deprecated:: 2.6
Ezio Melotti510ff542012-05-03 19:21:40 +0300206 The :class:`MutableString` class has been removed in Python 3.
Brett Cannonabb34fe2008-05-29 05:08:50 +0000207
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208In addition to supporting the methods and operations of string and Unicode
209objects (see section :ref:`string-methods`), :class:`UserString` instances
210provide 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