blob: ef643cc50a9d47f01f351bae32973c80adc70525 [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
Raymond Hettinger31043cd2005-01-04 21:25:00 +00008The 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
Raymond Hettinger79947162002-11-15 06:46:14 +000011dictionaries (such as the shelve module).
12
Raymond Hettinger31043cd2005-01-04 21:25:00 +000013This 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
Raymond Hettinger79947162002-11-15 06:46:14 +000021The \module{UserDict} module defines the \class{UserDict} class
22and \class{DictMixin}:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000023
Thomas Woutersf8316632000-07-16 19:01:10 +000024\begin{classdesc}{UserDict}{\optional{initialdata}}
Fred Drake2c4f5542000-10-10 22:00:03 +000025Class that simulates a dictionary. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000026contents are kept in a regular dictionary, which is accessible via the
Fred Drake8d212431999-07-26 15:45:52 +000027\member{data} attribute of \class{UserDict} instances. If
28\var{initialdata} is provided, \member{data} is initialized with its
29contents; note that a reference to \var{initialdata} will not be kept,
Raymond Hettinger999b57c2003-08-25 04:28:05 +000030allowing it be used for other purposes.
Fred Drake802a2021998-02-19 06:26:35 +000031\end{classdesc}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000032
Fred Drake6bf37de1999-06-29 18:13:37 +000033In addition to supporting the methods and operations of mappings (see
34section \ref{typesmapping}), \class{UserDict} instances provide the
35following attribute:
36
Fred Drake1ce36041998-04-07 20:05:33 +000037\begin{memberdesc}{data}
38A real dictionary used to store the contents of the \class{UserDict}
39class.
40\end{memberdesc}
41
Raymond Hettinger79947162002-11-15 06:46:14 +000042\begin{classdesc}{DictMixin}{}
43Mixin defining all dictionary methods for classes that already have
Raymond Hettinger8ddc1762002-11-18 04:34:10 +000044a minimum dictionary interface including \method{__getitem__()},
45\method{__setitem__()}, \method{__delitem__()}, and \method{keys()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000046
47This mixin should be used as a superclass. Adding each of the
48above methods adds progressively more functionality. For instance,
Raymond Hettinger8ddc1762002-11-18 04:34:10 +000049defining all but \method{__delitem__} will preclude only \method{pop}
50and \method{popitem} from the full interface.
Raymond Hettinger79947162002-11-15 06:46:14 +000051
Raymond Hettinger68804312005-01-01 00:28:46 +000052In addition to the four base methods, progressively more efficiency
Raymond Hettinger8ddc1762002-11-18 04:34:10 +000053comes with defining \method{__contains__()}, \method{__iter__()}, and
54\method{iteritems()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000055
Raymond Hettinger8ddc1762002-11-18 04:34:10 +000056Since the mixin has no knowledge of the subclass constructor, it
57does not define \method{__init__()} or \method{copy()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000058\end{classdesc}
59
Fred Drake1ce36041998-04-07 20:05:33 +000060
Fred Drake295da241998-08-10 19:42:37 +000061\section{\module{UserList} ---
Fred Drake6bf37de1999-06-29 18:13:37 +000062 Class wrapper for list objects}
Fred Drakeb91e9341998-07-23 17:59:49 +000063
Fred Drake6bf37de1999-06-29 18:13:37 +000064\declaremodule{standard}{UserList}
Fred Drakeb91e9341998-07-23 17:59:49 +000065\modulesynopsis{Class wrapper for list objects.}
66
Fred Drake1ce36041998-04-07 20:05:33 +000067
Fred Draked5be3b72001-10-26 18:37:27 +000068\note{This module is available for backward compatibility only. If
69you are writing code that does not need to work with versions of
70Python earlier than Python 2.2, please consider subclassing directly
71from the built-in \class{list} type.}
72
Fred Drake1ce36041998-04-07 20:05:33 +000073This module defines a class that acts as a wrapper around
74list objects. It is a useful base class for
75your own list-like classes, which can inherit from
76them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000077can add new behaviors to lists.
Fred Drake1ce36041998-04-07 20:05:33 +000078
Fred Drake802a2021998-02-19 06:26:35 +000079The \module{UserList} module defines the \class{UserList} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000080
Fred Drake802a2021998-02-19 06:26:35 +000081\begin{classdesc}{UserList}{\optional{list}}
Fred Drake2c4f5542000-10-10 22:00:03 +000082Class that simulates a list. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000083contents are kept in a regular list, which is accessible via the
Fred Drake802a2021998-02-19 06:26:35 +000084\member{data} attribute of \class{UserList} instances. The instance's
Fred Drakefcda5601998-01-07 22:05:25 +000085contents are initially set to a copy of \var{list}, defaulting to the
Guido van Rossum7f3b0421997-03-27 14:56:18 +000086empty list \code{[]}. \var{list} can be either a regular Python list,
Fred Drake802a2021998-02-19 06:26:35 +000087or an instance of \class{UserList} (or a subclass).
88\end{classdesc}
Fred Drake1ce36041998-04-07 20:05:33 +000089
Fred Drake6bf37de1999-06-29 18:13:37 +000090In addition to supporting the methods and operations of mutable
91sequences (see section \ref{typesseq}), \class{UserList} instances
92provide the following attribute:
93
Fred Drake1ce36041998-04-07 20:05:33 +000094\begin{memberdesc}{data}
95A real Python list object used to store the contents of the
96\class{UserList} class.
97\end{memberdesc}
Fred Drakea22b5762000-04-03 03:51:50 +000098
Fred Drakec6243e42000-10-06 20:04:48 +000099\strong{Subclassing requirements:}
100Subclasses of \class{UserList} are expect to offer a constructor which
101can be called with either no arguments or one argument. List
102operations which return a new sequence attempt to create an instance
103of the actual implementation class. To do so, it assumes that the
104constructor can be called with a single parameter, which is a sequence
105object used as a data source.
106
107If a derived class does not wish to comply with this requirement, all
108of the special methods supported by this class will need to be
109overridden; please consult the sources for information about the
110methods which need to be provided in that case.
111
112\versionchanged[Python versions 1.5.2 and 1.6 also required that the
113 constructor be callable with no parameters, and offer
114 a mutable \member{data} attribute. Earlier versions
115 of Python did not attempt to create instances of the
116 derived class]{2.0}
117
Fred Drakea22b5762000-04-03 03:51:50 +0000118
119\section{\module{UserString} ---
120 Class wrapper for string objects}
121
122\declaremodule{standard}{UserString}
123\modulesynopsis{Class wrapper for string objects.}
124\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
125\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
126
Fred Draked5be3b72001-10-26 18:37:27 +0000127\note{This \class{UserString} class from this module is available for
128backward compatibility only. If you are writing code that does not
129need to work with versions of Python earlier than Python 2.2, please
130consider subclassing directly from the built-in \class{str} type
131instead of using \class{UserString} (there is no built-in equivalent
132to \class{MutableString}).}
133
Fred Drake66c9f072000-10-10 20:58:48 +0000134This module defines a class that acts as a wrapper around string
135objects. It is a useful base class for your own string-like classes,
136which can inherit from them and override existing methods or add new
137ones. In this way one can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +0000138
Fred Drake66c9f072000-10-10 20:58:48 +0000139It should be noted that these classes are highly inefficient compared
140to real string or Unicode objects; this is especially the case for
141\class{MutableString}.
142
143The \module{UserString} module defines the following classes:
Fred Drakea22b5762000-04-03 03:51:50 +0000144
145\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake2c4f5542000-10-10 22:00:03 +0000146Class that simulates a string or a Unicode string
Fred Drake66c9f072000-10-10 20:58:48 +0000147object. The instance's content is kept in a regular string or Unicode
148string object, which is accessible via the \member{data} attribute of
149\class{UserString} instances. The instance's contents are initially
150set to a copy of \var{sequence}. \var{sequence} can be either a
151regular Python string or Unicode string, an instance of
152\class{UserString} (or a subclass) or an arbitrary sequence which can
153be converted into a string using the built-in \function{str()} function.
Fred Drakea22b5762000-04-03 03:51:50 +0000154\end{classdesc}
155
Fred Drakea22b5762000-04-03 03:51:50 +0000156\begin{classdesc}{MutableString}{\optional{sequence}}
157This class is derived from the \class{UserString} above and redefines
158strings to be \emph{mutable}. Mutable strings can't be used as
159dictionary keys, because dictionaries require \emph{immutable} objects as
160keys. The main intention of this class is to serve as an educational
161example for inheritance and necessity to remove (override) the
Fred Drake66c9f072000-10-10 20:58:48 +0000162\method{__hash__()} method in order to trap attempts to use a
Fred Drakea22b5762000-04-03 03:51:50 +0000163mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000164error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000165\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000166
Fred Drake66c9f072000-10-10 20:58:48 +0000167In addition to supporting the methods and operations of string and
168Unicode objects (see section \ref{string-methods}, ``String
169Methods''), \class{UserString} instances provide the following
170attribute:
Fred Drake621d2be2000-09-09 03:23:50 +0000171
172\begin{memberdesc}{data}
173A real Python string or Unicode object used to store the content of the
174\class{UserString} class.
175\end{memberdesc}