blob: 0490118d316c0cc19307144b19931ce64e6ddd51 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
14This also module defines a class, :class:`UserDict`, that acts as a wrapper
15around 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
22The :mod:`UserDict` module defines the :class:`UserDict` class and
23:class:`DictMixin`:
24
25
26.. class:: UserDict([initialdata])
27
28 Class that simulates a dictionary. The instance's contents are kept in a
29 regular dictionary, which is accessible via the :attr:`data` attribute of
30 :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is
31 initialized with its contents; note that a reference to *initialdata* will not
32 be kept, allowing it be used for other purposes.
33
34 .. note::
35
Fred Drake2e748782007-09-04 17:33:11 +000036 For backward compatibility, instances of :class:`UserDict` are not
37 iterable.
Georg Brandl116aa622007-08-15 14:28:22 +000038
39
40.. class:: IterableUserDict([initialdata])
41
42 Subclass of :class:`UserDict` that supports direct iteration (e.g. ``for key in
43 myDict``).
44
45In addition to supporting the methods and operations of mappings (see section
46:ref:`typesmapping`), :class:`UserDict` and :class:`IterableUserDict` instances
47provide the following attribute:
48
49
50.. attribute:: IterableUserDict.data
51
52 A real dictionary used to store the contents of the :class:`UserDict` class.
53
54
55.. class:: DictMixin()
56
57 Mixin defining all dictionary methods for classes that already have a minimum
58 dictionary interface including :meth:`__getitem__`, :meth:`__setitem__`,
59 :meth:`__delitem__`, and :meth:`keys`.
60
61 This mixin should be used as a superclass. Adding each of the above methods
62 adds progressively more functionality. For instance, defining all but
63 :meth:`__delitem__` will preclude only :meth:`pop` and :meth:`popitem` from the
64 full interface.
65
Fred Drake2e748782007-09-04 17:33:11 +000066 In addition to the four base methods, progressively more efficiency comes
67 with defining :meth:`__contains__` and :meth:`__iter__`.
Georg Brandl116aa622007-08-15 14:28:22 +000068
69 Since the mixin has no knowledge of the subclass constructor, it does not define
70 :meth:`__init__` or :meth:`copy`.
71
72
73:mod:`UserList` --- Class wrapper for list objects
74==================================================
75
76.. module:: UserList
77 :synopsis: Class wrapper for list objects.
78
79
80.. note::
81
82 This module is available for backward compatibility only. If you are writing
83 code that does not need to work with versions of Python earlier than Python 2.2,
84 please consider subclassing directly from the built-in :class:`list` type.
85
86This module defines a class that acts as a wrapper around list objects. It is a
87useful base class for your own list-like classes, which can inherit from them
88and override existing methods or add new ones. In this way one can add new
89behaviors to lists.
90
91The :mod:`UserList` module defines the :class:`UserList` class:
92
93
94.. class:: UserList([list])
95
96 Class that simulates a list. The instance's contents are kept in a regular
Fred Drake2e748782007-09-04 17:33:11 +000097 list, which is accessible via the :attr:`data` attribute of
98 :class:`UserList`
Georg Brandl116aa622007-08-15 14:28:22 +000099 instances. The instance's contents are initially set to a copy of *list*,
Fred Drake2e748782007-09-04 17:33:11 +0000100 defaulting to the empty list ``[]``. *list* can be any iterable, for
101 example a real Python list or a :class:`UserList` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103In addition to supporting the methods and operations of mutable sequences (see
104section :ref:`typesseq`), :class:`UserList` instances provide the following
105attribute:
106
107
108.. attribute:: UserList.data
109
110 A real Python list object used to store the contents of the :class:`UserList`
111 class.
112
113**Subclassing requirements:** Subclasses of :class:`UserList` are expect to
114offer a constructor which can be called with either no arguments or one
115argument. List operations which return a new sequence attempt to create an
116instance of the actual implementation class. To do so, it assumes that the
117constructor can be called with a single parameter, which is a sequence object
118used as a data source.
119
120If a derived class does not wish to comply with this requirement, all of the
121special methods supported by this class will need to be overridden; please
122consult the sources for information about the methods which need to be provided
123in that case.
124
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126:mod:`UserString` --- Class wrapper for string objects
127======================================================
128
129.. module:: UserString
130 :synopsis: Class wrapper for string objects.
131.. moduleauthor:: Peter Funk <pf@artcom-gmbh.de>
132.. sectionauthor:: Peter Funk <pf@artcom-gmbh.de>
133
134
135.. note::
136
137 This :class:`UserString` class from this module is available for backward
138 compatibility only. If you are writing code that does not need to work with
139 versions of Python earlier than Python 2.2, please consider subclassing directly
140 from the built-in :class:`str` type instead of using :class:`UserString` (there
141 is no built-in equivalent to :class:`MutableString`).
142
143This module defines a class that acts as a wrapper around string objects. It is
144a useful base class for your own string-like classes, which can inherit from
145them and override existing methods or add new ones. In this way one can add new
146behaviors to strings.
147
148It should be noted that these classes are highly inefficient compared to real
149string or Unicode objects; this is especially the case for
150:class:`MutableString`.
151
152The :mod:`UserString` module defines the following classes:
153
154
155.. class:: UserString([sequence])
156
157 Class that simulates a string or a Unicode string object. The instance's
158 content is kept in a regular string or Unicode string object, which is
159 accessible via the :attr:`data` attribute of :class:`UserString` instances. The
160 instance's contents are initially set to a copy of *sequence*. *sequence* can
161 be either a regular Python string or Unicode string, an instance of
162 :class:`UserString` (or a subclass) or an arbitrary sequence which can be
163 converted into a string using the built-in :func:`str` function.
164
165
166.. class:: MutableString([sequence])
167
168 This class is derived from the :class:`UserString` above and redefines strings
169 to be *mutable*. Mutable strings can't be used as dictionary keys, because
170 dictionaries require *immutable* objects as keys. The main intention of this
171 class is to serve as an educational example for inheritance and necessity to
172 remove (override) the :meth:`__hash__` method in order to trap attempts to use a
173 mutable object as dictionary key, which would be otherwise very error prone and
174 hard to track down.
175
176In addition to supporting the methods and operations of string and Unicode
177objects (see section :ref:`string-methods`), :class:`UserString` instances
178provide the following attribute:
179
180
181.. attribute:: MutableString.data
182
183 A real Python string or Unicode object used to store the content of the
184 :class:`UserString` class.
185