blob: 62aff77d4e6f4c9b0773bf9b4e2a3a4c603dc61c [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 Drake1ce36041998-04-07 20:05:33 +00007This module defines a class that acts as a wrapper around
8dictionary objects. It is a useful base class for
9your own dictionary-like classes, which can inherit from
Guido van Rossum7f3b0421997-03-27 14:56:18 +000010them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000011can add new behaviors to dictionaries.
Guido van Rossum7f3b0421997-03-27 14:56:18 +000012
Fred Drake802a2021998-02-19 06:26:35 +000013The \module{UserDict} module defines the \class{UserDict} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000014
Thomas Woutersf8316632000-07-16 19:01:10 +000015\begin{classdesc}{UserDict}{\optional{initialdata}}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000016Return a class instance that simulates a dictionary. The instance's
17contents are kept in a regular dictionary, which is accessible via the
Fred Drake8d212431999-07-26 15:45:52 +000018\member{data} attribute of \class{UserDict} instances. If
19\var{initialdata} is provided, \member{data} is initialized with its
20contents; note that a reference to \var{initialdata} will not be kept,
21allowing it be used used for other purposes.
Fred Drake802a2021998-02-19 06:26:35 +000022\end{classdesc}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000023
Fred Drake6bf37de1999-06-29 18:13:37 +000024In addition to supporting the methods and operations of mappings (see
25section \ref{typesmapping}), \class{UserDict} instances provide the
26following attribute:
27
Fred Drake1ce36041998-04-07 20:05:33 +000028\begin{memberdesc}{data}
29A real dictionary used to store the contents of the \class{UserDict}
30class.
31\end{memberdesc}
32
33
Fred Drake295da241998-08-10 19:42:37 +000034\section{\module{UserList} ---
Fred Drake6bf37de1999-06-29 18:13:37 +000035 Class wrapper for list objects}
Fred Drakeb91e9341998-07-23 17:59:49 +000036
Fred Drake6bf37de1999-06-29 18:13:37 +000037\declaremodule{standard}{UserList}
Fred Drakeb91e9341998-07-23 17:59:49 +000038\modulesynopsis{Class wrapper for list objects.}
39
Fred Drake1ce36041998-04-07 20:05:33 +000040
41This module defines a class that acts as a wrapper around
42list objects. It is a useful base class for
43your own list-like classes, which can inherit from
44them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000045can add new behaviors to lists.
Fred Drake1ce36041998-04-07 20:05:33 +000046
Fred Drake802a2021998-02-19 06:26:35 +000047The \module{UserList} module defines the \class{UserList} class:
Guido van Rossum7f3b0421997-03-27 14:56:18 +000048
Fred Drake802a2021998-02-19 06:26:35 +000049\begin{classdesc}{UserList}{\optional{list}}
Guido van Rossum7f3b0421997-03-27 14:56:18 +000050Return a class instance that simulates a list. The instance's
51contents are kept in a regular list, which is accessible via the
Fred Drake802a2021998-02-19 06:26:35 +000052\member{data} attribute of \class{UserList} instances. The instance's
Fred Drakefcda5601998-01-07 22:05:25 +000053contents are initially set to a copy of \var{list}, defaulting to the
Guido van Rossum7f3b0421997-03-27 14:56:18 +000054empty list \code{[]}. \var{list} can be either a regular Python list,
Fred Drake802a2021998-02-19 06:26:35 +000055or an instance of \class{UserList} (or a subclass).
56\end{classdesc}
Fred Drake1ce36041998-04-07 20:05:33 +000057
Fred Drake6bf37de1999-06-29 18:13:37 +000058In addition to supporting the methods and operations of mutable
59sequences (see section \ref{typesseq}), \class{UserList} instances
60provide the following attribute:
61
Fred Drake1ce36041998-04-07 20:05:33 +000062\begin{memberdesc}{data}
63A real Python list object used to store the contents of the
64\class{UserList} class.
65\end{memberdesc}
Fred Drakea22b5762000-04-03 03:51:50 +000066
Fred Drakec6243e42000-10-06 20:04:48 +000067\strong{Subclassing requirements:}
68Subclasses of \class{UserList} are expect to offer a constructor which
69can be called with either no arguments or one argument. List
70operations which return a new sequence attempt to create an instance
71of the actual implementation class. To do so, it assumes that the
72constructor can be called with a single parameter, which is a sequence
73object used as a data source.
74
75If a derived class does not wish to comply with this requirement, all
76of the special methods supported by this class will need to be
77overridden; please consult the sources for information about the
78methods which need to be provided in that case.
79
80\versionchanged[Python versions 1.5.2 and 1.6 also required that the
81 constructor be callable with no parameters, and offer
82 a mutable \member{data} attribute. Earlier versions
83 of Python did not attempt to create instances of the
84 derived class]{2.0}
85
Fred Drakea22b5762000-04-03 03:51:50 +000086
87\section{\module{UserString} ---
88 Class wrapper for string objects}
89
90\declaremodule{standard}{UserString}
91\modulesynopsis{Class wrapper for string objects.}
92\moduleauthor{Peter Funk}{pf@artcom-gmbh.de}
93\sectionauthor{Peter Funk}{pf@artcom-gmbh.de}
94
95This module defines a class that acts as a wrapper around
96string objects. It is a useful base class for
97your own string-like classes, which can inherit from
98them and override existing methods or add new ones. In this way one
Thomas Woutersf8316632000-07-16 19:01:10 +000099can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +0000100
101The \module{UserString} module defines the \class{UserString} class:
102
103\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake6a0d8442000-04-03 15:02:35 +0000104Return a class instance that simulates a string or a Unicode string object.
105The instance's content is kept in a regular string or Unicode string
Fred Drakea22b5762000-04-03 03:51:50 +0000106object, which is accessible via the
107\member{data} attribute of \class{UserString} instances. The instance's
108contents are initially set to a copy of \var{sequence}.
Fred Drake6a0d8442000-04-03 15:02:35 +0000109\var{sequence} can be either a regular Python string or Unicode string,
Fred Drakea22b5762000-04-03 03:51:50 +0000110an instance of \class{UserString} (or a subclass) or an arbitrary sequence
111which can be converted into a string.
Fred Drakea22b5762000-04-03 03:51:50 +0000112\end{classdesc}
113
Fred Drakea22b5762000-04-03 03:51:50 +0000114\begin{classdesc}{MutableString}{\optional{sequence}}
115This class is derived from the \class{UserString} above and redefines
116strings to be \emph{mutable}. Mutable strings can't be used as
117dictionary keys, because dictionaries require \emph{immutable} objects as
118keys. The main intention of this class is to serve as an educational
119example for inheritance and necessity to remove (override) the
120\function{__hash__} method in order to trap attempts to use a
121mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000122error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000123\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000124
125In addition to supporting the methods and operations of string or
126Unicode objects (see section \ref{typesseq}), \class{UserString} instances
127provide the following attribute:
128
129\begin{memberdesc}{data}
130A real Python string or Unicode object used to store the content of the
131\class{UserString} class.
132\end{memberdesc}