blob: 7e6d6669257bd1edb077a50292bffd5d2eb04e10 [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
10from the built-in \class{dictionary} type.}
11
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
Fred Drake802a2021998-02-19 06:26:35 +000018The \module{UserDict} module defines the \class{UserDict} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000019
Thomas Woutersf8316632000-07-16 19:01:10 +000020\begin{classdesc}{UserDict}{\optional{initialdata}}
Fred Drake2c4f5542000-10-10 22:00:03 +000021Class that simulates a dictionary. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000022contents are kept in a regular dictionary, which is accessible via the
Fred Drake8d212431999-07-26 15:45:52 +000023\member{data} attribute of \class{UserDict} instances. If
24\var{initialdata} is provided, \member{data} is initialized with its
25contents; note that a reference to \var{initialdata} will not be kept,
26allowing it be used used for other purposes.
Fred Drake802a2021998-02-19 06:26:35 +000027\end{classdesc}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000028
Fred Drake6bf37de1999-06-29 18:13:37 +000029In addition to supporting the methods and operations of mappings (see
30section \ref{typesmapping}), \class{UserDict} instances provide the
31following attribute:
32
Fred Drake1ce36041998-04-07 20:05:33 +000033\begin{memberdesc}{data}
34A real dictionary used to store the contents of the \class{UserDict}
35class.
36\end{memberdesc}
37
38
Fred Drake295da241998-08-10 19:42:37 +000039\section{\module{UserList} ---
Fred Drake6bf37de1999-06-29 18:13:37 +000040 Class wrapper for list objects}
Fred Drakeb91e9341998-07-23 17:59:49 +000041
Fred Drake6bf37de1999-06-29 18:13:37 +000042\declaremodule{standard}{UserList}
Fred Drakeb91e9341998-07-23 17:59:49 +000043\modulesynopsis{Class wrapper for list objects.}
44
Fred Drake1ce36041998-04-07 20:05:33 +000045
Fred Draked5be3b72001-10-26 18:37:27 +000046\note{This module is available for backward compatibility only. If
47you are writing code that does not need to work with versions of
48Python earlier than Python 2.2, please consider subclassing directly
49from the built-in \class{list} type.}
50
Fred Drake1ce36041998-04-07 20:05:33 +000051This module defines a class that acts as a wrapper around
52list objects. It is a useful base class for
53your own list-like classes, which can inherit from
54them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000055can add new behaviors to lists.
Fred Drake1ce36041998-04-07 20:05:33 +000056
Fred Drake802a2021998-02-19 06:26:35 +000057The \module{UserList} module defines the \class{UserList} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000058
Fred Drake802a2021998-02-19 06:26:35 +000059\begin{classdesc}{UserList}{\optional{list}}
Fred Drake2c4f5542000-10-10 22:00:03 +000060Class that simulates a list. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000061contents are kept in a regular list, which is accessible via the
Fred Drake802a2021998-02-19 06:26:35 +000062\member{data} attribute of \class{UserList} instances. The instance's
Fred Drakefcda5601998-01-07 22:05:25 +000063contents are initially set to a copy of \var{list}, defaulting to the
Guido van Rossum7f3b0421997-03-27 14:56:18 +000064empty list \code{[]}. \var{list} can be either a regular Python list,
Fred Drake802a2021998-02-19 06:26:35 +000065or an instance of \class{UserList} (or a subclass).
66\end{classdesc}
Fred Drake1ce36041998-04-07 20:05:33 +000067
Fred Drake6bf37de1999-06-29 18:13:37 +000068In addition to supporting the methods and operations of mutable
69sequences (see section \ref{typesseq}), \class{UserList} instances
70provide the following attribute:
71
Fred Drake1ce36041998-04-07 20:05:33 +000072\begin{memberdesc}{data}
73A real Python list object used to store the contents of the
74\class{UserList} class.
75\end{memberdesc}
Fred Drakea22b5762000-04-03 03:51:50 +000076
Fred Drakec6243e42000-10-06 20:04:48 +000077\strong{Subclassing requirements:}
78Subclasses of \class{UserList} are expect to offer a constructor which
79can be called with either no arguments or one argument. List
80operations which return a new sequence attempt to create an instance
81of the actual implementation class. To do so, it assumes that the
82constructor can be called with a single parameter, which is a sequence
83object used as a data source.
84
85If a derived class does not wish to comply with this requirement, all
86of the special methods supported by this class will need to be
87overridden; please consult the sources for information about the
88methods which need to be provided in that case.
89
90\versionchanged[Python versions 1.5.2 and 1.6 also required that the
91 constructor be callable with no parameters, and offer
92 a mutable \member{data} attribute. Earlier versions
93 of Python did not attempt to create instances of the
94 derived class]{2.0}
95
Fred Drakea22b5762000-04-03 03:51:50 +000096
97\section{\module{UserString} ---
98 Class wrapper for string objects}
99
100\declaremodule{standard}{UserString}
101\modulesynopsis{Class wrapper for string objects.}
102\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
103\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
104
Fred Draked5be3b72001-10-26 18:37:27 +0000105\note{This \class{UserString} class from this module is available for
106backward compatibility only. If you are writing code that does not
107need to work with versions of Python earlier than Python 2.2, please
108consider subclassing directly from the built-in \class{str} type
109instead of using \class{UserString} (there is no built-in equivalent
110to \class{MutableString}).}
111
Fred Drake66c9f072000-10-10 20:58:48 +0000112This module defines a class that acts as a wrapper around string
113objects. It is a useful base class for your own string-like classes,
114which can inherit from them and override existing methods or add new
115ones. In this way one can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +0000116
Fred Drake66c9f072000-10-10 20:58:48 +0000117It should be noted that these classes are highly inefficient compared
118to real string or Unicode objects; this is especially the case for
119\class{MutableString}.
120
121The \module{UserString} module defines the following classes:
Fred Drakea22b5762000-04-03 03:51:50 +0000122
123\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake2c4f5542000-10-10 22:00:03 +0000124Class that simulates a string or a Unicode string
Fred Drake66c9f072000-10-10 20:58:48 +0000125object. The instance's content is kept in a regular string or Unicode
126string object, which is accessible via the \member{data} attribute of
127\class{UserString} instances. The instance's contents are initially
128set to a copy of \var{sequence}. \var{sequence} can be either a
129regular Python string or Unicode string, an instance of
130\class{UserString} (or a subclass) or an arbitrary sequence which can
131be converted into a string using the built-in \function{str()} function.
Fred Drakea22b5762000-04-03 03:51:50 +0000132\end{classdesc}
133
Fred Drakea22b5762000-04-03 03:51:50 +0000134\begin{classdesc}{MutableString}{\optional{sequence}}
135This class is derived from the \class{UserString} above and redefines
136strings to be \emph{mutable}. Mutable strings can't be used as
137dictionary keys, because dictionaries require \emph{immutable} objects as
138keys. The main intention of this class is to serve as an educational
139example for inheritance and necessity to remove (override) the
Fred Drake66c9f072000-10-10 20:58:48 +0000140\method{__hash__()} method in order to trap attempts to use a
Fred Drakea22b5762000-04-03 03:51:50 +0000141mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000142error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000143\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000144
Fred Drake66c9f072000-10-10 20:58:48 +0000145In addition to supporting the methods and operations of string and
146Unicode objects (see section \ref{string-methods}, ``String
147Methods''), \class{UserString} instances provide the following
148attribute:
Fred Drake621d2be2000-09-09 03:23:50 +0000149
150\begin{memberdesc}{data}
151A real Python string or Unicode object used to store the content of the
152\class{UserString} class.
153\end{memberdesc}