blob: e01c546c75e115743376288b55e9d2cea1b14983 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{UserDict} ---
Fred Drake6bf37de1999-06-29 18:13:37 +00002 Class wrapper for dictionary objects}
3
Fred Drakeb91e9341998-07-23 17:59:49 +00004\declaremodule{standard}{UserDict}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Class wrapper for dictionary objects.}
6
Fred Draked5be3b72001-10-26 18:37:27 +00007\note{This module is available for backward compatibility only. If
8you are writing code that does not need to work with versions of
9Python earlier than Python 2.2, please consider subclassing directly
Raymond Hettinger8a9e8b62002-06-30 04:32:38 +000010from the built-in \class{dict} type.}
Fred Draked5be3b72001-10-26 18:37:27 +000011
Fred Drake1ce36041998-04-07 20:05:33 +000012This module defines a class that acts as a wrapper around
13dictionary objects. It is a useful base class for
14your own dictionary-like classes, which can inherit from
Guido van Rossum7f3b0421997-03-27 14:56:18 +000015them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000016can add new behaviors to dictionaries.
Guido van Rossum7f3b0421997-03-27 14:56:18 +000017
Raymond Hettinger79947162002-11-15 06:46:14 +000018The module also defines a mixin defining all dictionary methods for
19classes that already have a minimum mapping interface. This greatly
20simplifies writing classes that need to be substitutable for
21dictionaries (such as the shelve module).
22
23The \module{UserDict} module defines the \class{UserDict} class
24and \class{DictMixin}:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000025
Thomas Woutersf8316632000-07-16 19:01:10 +000026\begin{classdesc}{UserDict}{\optional{initialdata}}
Fred Drake2c4f5542000-10-10 22:00:03 +000027Class that simulates a dictionary. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000028contents are kept in a regular dictionary, which is accessible via the
Fred Drake8d212431999-07-26 15:45:52 +000029\member{data} attribute of \class{UserDict} instances. If
30\var{initialdata} is provided, \member{data} is initialized with its
31contents; note that a reference to \var{initialdata} will not be kept,
32allowing it be used used for other purposes.
Fred Drake802a2021998-02-19 06:26:35 +000033\end{classdesc}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000034
Fred Drake6bf37de1999-06-29 18:13:37 +000035In addition to supporting the methods and operations of mappings (see
36section \ref{typesmapping}), \class{UserDict} instances provide the
37following attribute:
38
Fred Drake1ce36041998-04-07 20:05:33 +000039\begin{memberdesc}{data}
40A real dictionary used to store the contents of the \class{UserDict}
41class.
42\end{memberdesc}
43
Raymond Hettinger79947162002-11-15 06:46:14 +000044\begin{classdesc}{DictMixin}{}
45Mixin defining all dictionary methods for classes that already have
46a minimum dictionary interface including\method{__getitem__},
47\method{__setitem__}, \method{__delitem__}, and \method{keys}.
48
49This mixin should be used as a superclass. Adding each of the
50above methods adds progressively more functionality. For instance,
51the absence of \method{__delitem__} precludes only \method{pop}
52and \method{popitem}.
53
54While the four methods listed above are sufficient to support the
55entire dictionary interface, progessively more efficiency comes
56with defining \method{__contains__}, \method{__iter__}, and
57\method{iteritems}.
58
59\end{classdesc}
60
Fred Drake1ce36041998-04-07 20:05:33 +000061
Fred Drake295da241998-08-10 19:42:37 +000062\section{\module{UserList} ---
Fred Drake6bf37de1999-06-29 18:13:37 +000063 Class wrapper for list objects}
Fred Drakeb91e9341998-07-23 17:59:49 +000064
Fred Drake6bf37de1999-06-29 18:13:37 +000065\declaremodule{standard}{UserList}
Fred Drakeb91e9341998-07-23 17:59:49 +000066\modulesynopsis{Class wrapper for list objects.}
67
Fred Drake1ce36041998-04-07 20:05:33 +000068
Fred Draked5be3b72001-10-26 18:37:27 +000069\note{This module is available for backward compatibility only. If
70you are writing code that does not need to work with versions of
71Python earlier than Python 2.2, please consider subclassing directly
72from the built-in \class{list} type.}
73
Fred Drake1ce36041998-04-07 20:05:33 +000074This module defines a class that acts as a wrapper around
75list objects. It is a useful base class for
76your own list-like classes, which can inherit from
77them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000078can add new behaviors to lists.
Fred Drake1ce36041998-04-07 20:05:33 +000079
Fred Drake802a2021998-02-19 06:26:35 +000080The \module{UserList} module defines the \class{UserList} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000081
Fred Drake802a2021998-02-19 06:26:35 +000082\begin{classdesc}{UserList}{\optional{list}}
Fred Drake2c4f5542000-10-10 22:00:03 +000083Class that simulates a list. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000084contents are kept in a regular list, which is accessible via the
Fred Drake802a2021998-02-19 06:26:35 +000085\member{data} attribute of \class{UserList} instances. The instance's
Fred Drakefcda5601998-01-07 22:05:25 +000086contents are initially set to a copy of \var{list}, defaulting to the
Guido van Rossum7f3b0421997-03-27 14:56:18 +000087empty list \code{[]}. \var{list} can be either a regular Python list,
Fred Drake802a2021998-02-19 06:26:35 +000088or an instance of \class{UserList} (or a subclass).
89\end{classdesc}
Fred Drake1ce36041998-04-07 20:05:33 +000090
Fred Drake6bf37de1999-06-29 18:13:37 +000091In addition to supporting the methods and operations of mutable
92sequences (see section \ref{typesseq}), \class{UserList} instances
93provide the following attribute:
94
Fred Drake1ce36041998-04-07 20:05:33 +000095\begin{memberdesc}{data}
96A real Python list object used to store the contents of the
97\class{UserList} class.
98\end{memberdesc}
Fred Drakea22b5762000-04-03 03:51:50 +000099
Fred Drakec6243e42000-10-06 20:04:48 +0000100\strong{Subclassing requirements:}
101Subclasses of \class{UserList} are expect to offer a constructor which
102can be called with either no arguments or one argument. List
103operations which return a new sequence attempt to create an instance
104of the actual implementation class. To do so, it assumes that the
105constructor can be called with a single parameter, which is a sequence
106object used as a data source.
107
108If a derived class does not wish to comply with this requirement, all
109of the special methods supported by this class will need to be
110overridden; please consult the sources for information about the
111methods which need to be provided in that case.
112
113\versionchanged[Python versions 1.5.2 and 1.6 also required that the
114 constructor be callable with no parameters, and offer
115 a mutable \member{data} attribute. Earlier versions
116 of Python did not attempt to create instances of the
117 derived class]{2.0}
118
Fred Drakea22b5762000-04-03 03:51:50 +0000119
120\section{\module{UserString} ---
121 Class wrapper for string objects}
122
123\declaremodule{standard}{UserString}
124\modulesynopsis{Class wrapper for string objects.}
125\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
126\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
127
Fred Draked5be3b72001-10-26 18:37:27 +0000128\note{This \class{UserString} class from this module is available for
129backward compatibility only. If you are writing code that does not
130need to work with versions of Python earlier than Python 2.2, please
131consider subclassing directly from the built-in \class{str} type
132instead of using \class{UserString} (there is no built-in equivalent
133to \class{MutableString}).}
134
Fred Drake66c9f072000-10-10 20:58:48 +0000135This module defines a class that acts as a wrapper around string
136objects. It is a useful base class for your own string-like classes,
137which can inherit from them and override existing methods or add new
138ones. In this way one can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +0000139
Fred Drake66c9f072000-10-10 20:58:48 +0000140It should be noted that these classes are highly inefficient compared
141to real string or Unicode objects; this is especially the case for
142\class{MutableString}.
143
144The \module{UserString} module defines the following classes:
Fred Drakea22b5762000-04-03 03:51:50 +0000145
146\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake2c4f5542000-10-10 22:00:03 +0000147Class that simulates a string or a Unicode string
Fred Drake66c9f072000-10-10 20:58:48 +0000148object. The instance's content is kept in a regular string or Unicode
149string object, which is accessible via the \member{data} attribute of
150\class{UserString} instances. The instance's contents are initially
151set to a copy of \var{sequence}. \var{sequence} can be either a
152regular Python string or Unicode string, an instance of
153\class{UserString} (or a subclass) or an arbitrary sequence which can
154be converted into a string using the built-in \function{str()} function.
Fred Drakea22b5762000-04-03 03:51:50 +0000155\end{classdesc}
156
Fred Drakea22b5762000-04-03 03:51:50 +0000157\begin{classdesc}{MutableString}{\optional{sequence}}
158This class is derived from the \class{UserString} above and redefines
159strings to be \emph{mutable}. Mutable strings can't be used as
160dictionary keys, because dictionaries require \emph{immutable} objects as
161keys. The main intention of this class is to serve as an educational
162example for inheritance and necessity to remove (override) the
Fred Drake66c9f072000-10-10 20:58:48 +0000163\method{__hash__()} method in order to trap attempts to use a
Fred Drakea22b5762000-04-03 03:51:50 +0000164mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000165error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000166\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000167
Fred Drake66c9f072000-10-10 20:58:48 +0000168In addition to supporting the methods and operations of string and
169Unicode objects (see section \ref{string-methods}, ``String
170Methods''), \class{UserString} instances provide the following
171attribute:
Fred Drake621d2be2000-09-09 03:23:50 +0000172
173\begin{memberdesc}{data}
174A real Python string or Unicode object used to store the content of the
175\class{UserString} class.
176\end{memberdesc}