blob: 144c0b4fa105b3f3fb54954a69b638d658be7bcd [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,
Raymond Hettinger999b57c2003-08-25 04:28:05 +000032allowing it be 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
Raymond Hettinger8ddc176e2002-11-18 04:34:10 +000046a minimum dictionary interface including \method{__getitem__()},
47\method{__setitem__()}, \method{__delitem__()}, and \method{keys()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000048
49This mixin should be used as a superclass. Adding each of the
50above methods adds progressively more functionality. For instance,
Raymond Hettinger8ddc176e2002-11-18 04:34:10 +000051defining all but \method{__delitem__} will preclude only \method{pop}
52and \method{popitem} from the full interface.
Raymond Hettinger79947162002-11-15 06:46:14 +000053
Raymond Hettinger8ddc176e2002-11-18 04:34:10 +000054In addition to the four base methods, progessively more efficiency
55comes with defining \method{__contains__()}, \method{__iter__()}, and
56\method{iteritems()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000057
Raymond Hettinger8ddc176e2002-11-18 04:34:10 +000058Since the mixin has no knowledge of the subclass constructor, it
59does not define \method{__init__()} or \method{copy()}.
Raymond Hettinger79947162002-11-15 06:46:14 +000060\end{classdesc}
61
Fred Drake1ce36041998-04-07 20:05:33 +000062
Fred Drake295da241998-08-10 19:42:37 +000063\section{\module{UserList} ---
Fred Drake6bf37de1999-06-29 18:13:37 +000064 Class wrapper for list objects}
Fred Drakeb91e9341998-07-23 17:59:49 +000065
Fred Drake6bf37de1999-06-29 18:13:37 +000066\declaremodule{standard}{UserList}
Fred Drakeb91e9341998-07-23 17:59:49 +000067\modulesynopsis{Class wrapper for list objects.}
68
Fred Drake1ce36041998-04-07 20:05:33 +000069
Fred Draked5be3b72001-10-26 18:37:27 +000070\note{This module is available for backward compatibility only. If
71you are writing code that does not need to work with versions of
72Python earlier than Python 2.2, please consider subclassing directly
73from the built-in \class{list} type.}
74
Fred Drake1ce36041998-04-07 20:05:33 +000075This module defines a class that acts as a wrapper around
76list objects. It is a useful base class for
77your own list-like classes, which can inherit from
78them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000079can add new behaviors to lists.
Fred Drake1ce36041998-04-07 20:05:33 +000080
Fred Drake802a2021998-02-19 06:26:35 +000081The \module{UserList} module defines the \class{UserList} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000082
Fred Drake802a2021998-02-19 06:26:35 +000083\begin{classdesc}{UserList}{\optional{list}}
Fred Drake2c4f5542000-10-10 22:00:03 +000084Class that simulates a list. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000085contents are kept in a regular list, which is accessible via the
Fred Drake802a2021998-02-19 06:26:35 +000086\member{data} attribute of \class{UserList} instances. The instance's
Fred Drakefcda5601998-01-07 22:05:25 +000087contents are initially set to a copy of \var{list}, defaulting to the
Guido van Rossum7f3b0421997-03-27 14:56:18 +000088empty list \code{[]}. \var{list} can be either a regular Python list,
Fred Drake802a2021998-02-19 06:26:35 +000089or an instance of \class{UserList} (or a subclass).
90\end{classdesc}
Fred Drake1ce36041998-04-07 20:05:33 +000091
Fred Drake6bf37de1999-06-29 18:13:37 +000092In addition to supporting the methods and operations of mutable
93sequences (see section \ref{typesseq}), \class{UserList} instances
94provide the following attribute:
95
Fred Drake1ce36041998-04-07 20:05:33 +000096\begin{memberdesc}{data}
97A real Python list object used to store the contents of the
98\class{UserList} class.
99\end{memberdesc}
Fred Drakea22b5762000-04-03 03:51:50 +0000100
Fred Drakec6243e42000-10-06 20:04:48 +0000101\strong{Subclassing requirements:}
102Subclasses of \class{UserList} are expect to offer a constructor which
103can be called with either no arguments or one argument. List
104operations which return a new sequence attempt to create an instance
105of the actual implementation class. To do so, it assumes that the
106constructor can be called with a single parameter, which is a sequence
107object used as a data source.
108
109If a derived class does not wish to comply with this requirement, all
110of the special methods supported by this class will need to be
111overridden; please consult the sources for information about the
112methods which need to be provided in that case.
113
114\versionchanged[Python versions 1.5.2 and 1.6 also required that the
115 constructor be callable with no parameters, and offer
116 a mutable \member{data} attribute. Earlier versions
117 of Python did not attempt to create instances of the
118 derived class]{2.0}
119
Fred Drakea22b5762000-04-03 03:51:50 +0000120
121\section{\module{UserString} ---
122 Class wrapper for string objects}
123
124\declaremodule{standard}{UserString}
125\modulesynopsis{Class wrapper for string objects.}
126\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
127\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
128
Fred Draked5be3b72001-10-26 18:37:27 +0000129\note{This \class{UserString} class from this module is available for
130backward compatibility only. If you are writing code that does not
131need to work with versions of Python earlier than Python 2.2, please
132consider subclassing directly from the built-in \class{str} type
133instead of using \class{UserString} (there is no built-in equivalent
134to \class{MutableString}).}
135
Fred Drake66c9f072000-10-10 20:58:48 +0000136This module defines a class that acts as a wrapper around string
137objects. It is a useful base class for your own string-like classes,
138which can inherit from them and override existing methods or add new
139ones. In this way one can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +0000140
Fred Drake66c9f072000-10-10 20:58:48 +0000141It should be noted that these classes are highly inefficient compared
142to real string or Unicode objects; this is especially the case for
143\class{MutableString}.
144
145The \module{UserString} module defines the following classes:
Fred Drakea22b5762000-04-03 03:51:50 +0000146
147\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake2c4f5542000-10-10 22:00:03 +0000148Class that simulates a string or a Unicode string
Fred Drake66c9f072000-10-10 20:58:48 +0000149object. The instance's content is kept in a regular string or Unicode
150string object, which is accessible via the \member{data} attribute of
151\class{UserString} instances. The instance's contents are initially
152set to a copy of \var{sequence}. \var{sequence} can be either a
153regular Python string or Unicode string, an instance of
154\class{UserString} (or a subclass) or an arbitrary sequence which can
155be converted into a string using the built-in \function{str()} function.
Fred Drakea22b5762000-04-03 03:51:50 +0000156\end{classdesc}
157
Fred Drakea22b5762000-04-03 03:51:50 +0000158\begin{classdesc}{MutableString}{\optional{sequence}}
159This class is derived from the \class{UserString} above and redefines
160strings to be \emph{mutable}. Mutable strings can't be used as
161dictionary keys, because dictionaries require \emph{immutable} objects as
162keys. The main intention of this class is to serve as an educational
163example for inheritance and necessity to remove (override) the
Fred Drake66c9f072000-10-10 20:58:48 +0000164\method{__hash__()} method in order to trap attempts to use a
Fred Drakea22b5762000-04-03 03:51:50 +0000165mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000166error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000167\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000168
Fred Drake66c9f072000-10-10 20:58:48 +0000169In addition to supporting the methods and operations of string and
170Unicode objects (see section \ref{string-methods}, ``String
171Methods''), \class{UserString} instances provide the following
172attribute:
Fred Drake621d2be2000-09-09 03:23:50 +0000173
174\begin{memberdesc}{data}
175A real Python string or Unicode object used to store the content of the
176\class{UserString} class.
177\end{memberdesc}