source: vendor/python/2.5/Doc/lib/libuserdict.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 7.6 KB
Line 
1\section{\module{UserDict} ---
2 Class wrapper for dictionary objects}
3
4\declaremodule{standard}{UserDict}
5\modulesynopsis{Class wrapper for dictionary objects.}
6
7
8The module defines a mixin, \class{DictMixin}, defining all dictionary
9methods for classes that already have a minimum mapping interface. This
10greatly simplifies writing classes that need to be substitutable for
11dictionaries (such as the shelve module).
12
13This also module defines a class, \class{UserDict}, that acts as a wrapper
14around dictionary objects. The need for this class has been largely
15supplanted by the ability to subclass directly from \class{dict} (a feature
16that became available starting with Python version 2.2). Prior to the
17introduction of \class{dict}, the \class{UserDict} class was used to
18create dictionary-like sub-classes that obtained new behaviors by overriding
19existing methods or adding new ones.
20
21The \module{UserDict} module defines the \class{UserDict} class
22and \class{DictMixin}:
23
24\begin{classdesc}{UserDict}{\optional{initialdata}}
25Class that simulates a dictionary. The instance's contents are kept
26in a regular dictionary, which is accessible via the \member{data}
27attribute of \class{UserDict} instances. If \var{initialdata} is
28provided, \member{data} is initialized with its contents; note that a
29reference to \var{initialdata} will not be kept, allowing it be used
30for other purposes. \note{For backward compatibility, instances of
31\class{UserDict} are not iterable.}
32\end{classdesc}
33
34\begin{classdesc}{IterableUserDict}{\optional{initialdata}}
35Subclass of \class{UserDict} that supports direct iteration (e.g.
36\code{for key in myDict}).
37\end{classdesc}
38
39In addition to supporting the methods and operations of mappings (see
40section \ref{typesmapping}), \class{UserDict} and
41\class{IterableUserDict} instances provide the following attribute:
42
43\begin{memberdesc}{data}
44A real dictionary used to store the contents of the \class{UserDict}
45class.
46\end{memberdesc}
47
48\begin{classdesc}{DictMixin}{}
49Mixin defining all dictionary methods for classes that already have
50a minimum dictionary interface including \method{__getitem__()},
51\method{__setitem__()}, \method{__delitem__()}, and \method{keys()}.
52
53This mixin should be used as a superclass. Adding each of the
54above methods adds progressively more functionality. For instance,
55defining all but \method{__delitem__} will preclude only \method{pop}
56and \method{popitem} from the full interface.
57
58In addition to the four base methods, progressively more efficiency
59comes with defining \method{__contains__()}, \method{__iter__()}, and
60\method{iteritems()}.
61
62Since the mixin has no knowledge of the subclass constructor, it
63does not define \method{__init__()} or \method{copy()}.
64\end{classdesc}
65
66
67\section{\module{UserList} ---
68 Class wrapper for list objects}
69
70\declaremodule{standard}{UserList}
71\modulesynopsis{Class wrapper for list objects.}
72
73
74\note{This module is available for backward compatibility only. If
75you are writing code that does not need to work with versions of
76Python earlier than Python 2.2, please consider subclassing directly
77from the built-in \class{list} type.}
78
79This module defines a class that acts as a wrapper around
80list objects. It is a useful base class for
81your own list-like classes, which can inherit from
82them and override existing methods or add new ones. In this way one
83can add new behaviors to lists.
84
85The \module{UserList} module defines the \class{UserList} class:
86
87\begin{classdesc}{UserList}{\optional{list}}
88Class that simulates a list. The instance's
89contents are kept in a regular list, which is accessible via the
90\member{data} attribute of \class{UserList} instances. The instance's
91contents are initially set to a copy of \var{list}, defaulting to the
92empty list \code{[]}. \var{list} can be either a regular Python list,
93or an instance of \class{UserList} (or a subclass).
94\end{classdesc}
95
96In addition to supporting the methods and operations of mutable
97sequences (see section \ref{typesseq}), \class{UserList} instances
98provide the following attribute:
99
100\begin{memberdesc}{data}
101A real Python list object used to store the contents of the
102\class{UserList} class.
103\end{memberdesc}
104
105\strong{Subclassing requirements:}
106Subclasses of \class{UserList} are expect to offer a constructor which
107can be called with either no arguments or one argument. List
108operations which return a new sequence attempt to create an instance
109of the actual implementation class. To do so, it assumes that the
110constructor can be called with a single parameter, which is a sequence
111object used as a data source.
112
113If a derived class does not wish to comply with this requirement, all
114of the special methods supported by this class will need to be
115overridden; please consult the sources for information about the
116methods which need to be provided in that case.
117
118\versionchanged[Python versions 1.5.2 and 1.6 also required that the
119 constructor be callable with no parameters, and offer
120 a mutable \member{data} attribute. Earlier versions
121 of Python did not attempt to create instances of the
122 derived class]{2.0}
123
124
125\section{\module{UserString} ---
126 Class wrapper for string objects}
127
128\declaremodule{standard}{UserString}
129\modulesynopsis{Class wrapper for string objects.}
130\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
131\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
132
133\note{This \class{UserString} class from this module is available for
134backward compatibility only. If you are writing code that does not
135need to work with versions of Python earlier than Python 2.2, please
136consider subclassing directly from the built-in \class{str} type
137instead of using \class{UserString} (there is no built-in equivalent
138to \class{MutableString}).}
139
140This module defines a class that acts as a wrapper around string
141objects. It is a useful base class for your own string-like classes,
142which can inherit from them and override existing methods or add new
143ones. In this way one can add new behaviors to strings.
144
145It should be noted that these classes are highly inefficient compared
146to real string or Unicode objects; this is especially the case for
147\class{MutableString}.
148
149The \module{UserString} module defines the following classes:
150
151\begin{classdesc}{UserString}{\optional{sequence}}
152Class that simulates a string or a Unicode string
153object. The instance's content is kept in a regular string or Unicode
154string object, which is accessible via the \member{data} attribute of
155\class{UserString} instances. The instance's contents are initially
156set to a copy of \var{sequence}. \var{sequence} can be either a
157regular Python string or Unicode string, an instance of
158\class{UserString} (or a subclass) or an arbitrary sequence which can
159be converted into a string using the built-in \function{str()} function.
160\end{classdesc}
161
162\begin{classdesc}{MutableString}{\optional{sequence}}
163This class is derived from the \class{UserString} above and redefines
164strings to be \emph{mutable}. Mutable strings can't be used as
165dictionary keys, because dictionaries require \emph{immutable} objects as
166keys. The main intention of this class is to serve as an educational
167example for inheritance and necessity to remove (override) the
168\method{__hash__()} method in order to trap attempts to use a
169mutable object as dictionary key, which would be otherwise very
170error prone and hard to track down.
171\end{classdesc}
172
173In addition to supporting the methods and operations of string and
174Unicode objects (see section \ref{string-methods}, ``String
175Methods''), \class{UserString} instances provide the following
176attribute:
177
178\begin{memberdesc}{data}
179A real Python string or Unicode object used to store the content of the
180\class{UserString} class.
181\end{memberdesc}
Note: See TracBrowser for help on using the repository browser.