blob: 0285380af957b146ed801a2fff60d1793e7362d4 [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}}
Fred Drake2c4f5542000-10-10 22:00:03 +000016Class that simulates a dictionary. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000017contents 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}}
Fred Drake2c4f5542000-10-10 22:00:03 +000050Class that simulates a list. The instance's
Guido van Rossum7f3b0421997-03-27 14:56:18 +000051contents 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
Fred Drake66c9f072000-10-10 20:58:48 +000095This module defines a class that acts as a wrapper around string
96objects. It is a useful base class for your own string-like classes,
97which can inherit from them and override existing methods or add new
98ones. In this way one can add new behaviors to strings.
Fred Drakea22b5762000-04-03 03:51:50 +000099
Fred Drake66c9f072000-10-10 20:58:48 +0000100It should be noted that these classes are highly inefficient compared
101to real string or Unicode objects; this is especially the case for
102\class{MutableString}.
103
104The \module{UserString} module defines the following classes:
Fred Drakea22b5762000-04-03 03:51:50 +0000105
106\begin{classdesc}{UserString}{\optional{sequence}}
Fred Drake2c4f5542000-10-10 22:00:03 +0000107Class that simulates a string or a Unicode string
Fred Drake66c9f072000-10-10 20:58:48 +0000108object. The instance's content is kept in a regular string or Unicode
109string object, which is accessible via the \member{data} attribute of
110\class{UserString} instances. The instance's contents are initially
111set to a copy of \var{sequence}. \var{sequence} can be either a
112regular Python string or Unicode string, an instance of
113\class{UserString} (or a subclass) or an arbitrary sequence which can
114be converted into a string using the built-in \function{str()} function.
Fred Drakea22b5762000-04-03 03:51:50 +0000115\end{classdesc}
116
Fred Drakea22b5762000-04-03 03:51:50 +0000117\begin{classdesc}{MutableString}{\optional{sequence}}
118This class is derived from the \class{UserString} above and redefines
119strings to be \emph{mutable}. Mutable strings can't be used as
120dictionary keys, because dictionaries require \emph{immutable} objects as
121keys. The main intention of this class is to serve as an educational
122example for inheritance and necessity to remove (override) the
Fred Drake66c9f072000-10-10 20:58:48 +0000123\method{__hash__()} method in order to trap attempts to use a
Fred Drakea22b5762000-04-03 03:51:50 +0000124mutable object as dictionary key, which would be otherwise very
Thomas Woutersf8316632000-07-16 19:01:10 +0000125error prone and hard to track down.
Fred Drakea22b5762000-04-03 03:51:50 +0000126\end{classdesc}
Fred Drake621d2be2000-09-09 03:23:50 +0000127
Fred Drake66c9f072000-10-10 20:58:48 +0000128In addition to supporting the methods and operations of string and
129Unicode objects (see section \ref{string-methods}, ``String
130Methods''), \class{UserString} instances provide the following
131attribute:
Fred Drake621d2be2000-09-09 03:23:50 +0000132
133\begin{memberdesc}{data}
134A real Python string or Unicode object used to store the content of the
135\class{UserString} class.
136\end{memberdesc}