Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{UserDict} --- |
Fred Drake | 6bf37de | 1999-06-29 18:13:37 +0000 | [diff] [blame] | 2 | Class wrapper for dictionary objects} |
| 3 | |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{UserDict} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Class wrapper for dictionary objects.} |
| 6 | |
Fred Drake | d5be3b7 | 2001-10-26 18:37:27 +0000 | [diff] [blame] | 7 | \note{This module is available for backward compatibility only. If |
| 8 | you are writing code that does not need to work with versions of |
| 9 | Python earlier than Python 2.2, please consider subclassing directly |
Raymond Hettinger | 8a9e8b6 | 2002-06-30 04:32:38 +0000 | [diff] [blame] | 10 | from the built-in \class{dict} type.} |
Fred Drake | d5be3b7 | 2001-10-26 18:37:27 +0000 | [diff] [blame] | 11 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 12 | This module defines a class that acts as a wrapper around |
| 13 | dictionary objects. It is a useful base class for |
| 14 | your own dictionary-like classes, which can inherit from |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 15 | them and override existing methods or add new ones. In this way one |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 16 | can add new behaviors to dictionaries. |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 17 | |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 18 | The module also defines a mixin defining all dictionary methods for |
| 19 | classes that already have a minimum mapping interface. This greatly |
| 20 | simplifies writing classes that need to be substitutable for |
| 21 | dictionaries (such as the shelve module). |
| 22 | |
| 23 | The \module{UserDict} module defines the \class{UserDict} class |
| 24 | and \class{DictMixin}: |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 25 | |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 26 | \begin{classdesc}{UserDict}{\optional{initialdata}} |
Fred Drake | 2c4f554 | 2000-10-10 22:00:03 +0000 | [diff] [blame] | 27 | Class that simulates a dictionary. The instance's |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 28 | contents are kept in a regular dictionary, which is accessible via the |
Fred Drake | 8d21243 | 1999-07-26 15:45:52 +0000 | [diff] [blame] | 29 | \member{data} attribute of \class{UserDict} instances. If |
| 30 | \var{initialdata} is provided, \member{data} is initialized with its |
| 31 | contents; note that a reference to \var{initialdata} will not be kept, |
| 32 | allowing it be used used for other purposes. |
Fred Drake | 802a202 | 1998-02-19 06:26:35 +0000 | [diff] [blame] | 33 | \end{classdesc} |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 34 | |
Fred Drake | 6bf37de | 1999-06-29 18:13:37 +0000 | [diff] [blame] | 35 | In addition to supporting the methods and operations of mappings (see |
| 36 | section \ref{typesmapping}), \class{UserDict} instances provide the |
| 37 | following attribute: |
| 38 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 39 | \begin{memberdesc}{data} |
| 40 | A real dictionary used to store the contents of the \class{UserDict} |
| 41 | class. |
| 42 | \end{memberdesc} |
| 43 | |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 44 | \begin{classdesc}{DictMixin}{} |
| 45 | Mixin defining all dictionary methods for classes that already have |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 46 | a minimum dictionary interface including \method{__getitem__()}, |
| 47 | \method{__setitem__()}, \method{__delitem__()}, and \method{keys()}. |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 48 | |
| 49 | This mixin should be used as a superclass. Adding each of the |
| 50 | above methods adds progressively more functionality. For instance, |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 51 | defining all but \method{__delitem__} will preclude only \method{pop} |
| 52 | and \method{popitem} from the full interface. |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 53 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 54 | In addition to the four base methods, progessively more efficiency |
| 55 | comes with defining \method{__contains__()}, \method{__iter__()}, and |
| 56 | \method{iteritems()}. |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 57 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 58 | Since the mixin has no knowledge of the subclass constructor, it |
| 59 | does not define \method{__init__()} or \method{copy()}. |
Raymond Hettinger | 7994716 | 2002-11-15 06:46:14 +0000 | [diff] [blame] | 60 | \end{classdesc} |
| 61 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 62 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 63 | \section{\module{UserList} --- |
Fred Drake | 6bf37de | 1999-06-29 18:13:37 +0000 | [diff] [blame] | 64 | Class wrapper for list objects} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 65 | |
Fred Drake | 6bf37de | 1999-06-29 18:13:37 +0000 | [diff] [blame] | 66 | \declaremodule{standard}{UserList} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 67 | \modulesynopsis{Class wrapper for list objects.} |
| 68 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 69 | |
Fred Drake | d5be3b7 | 2001-10-26 18:37:27 +0000 | [diff] [blame] | 70 | \note{This module is available for backward compatibility only. If |
| 71 | you are writing code that does not need to work with versions of |
| 72 | Python earlier than Python 2.2, please consider subclassing directly |
| 73 | from the built-in \class{list} type.} |
| 74 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 75 | This module defines a class that acts as a wrapper around |
| 76 | list objects. It is a useful base class for |
| 77 | your own list-like classes, which can inherit from |
| 78 | them and override existing methods or add new ones. In this way one |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 79 | can add new behaviors to lists. |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 80 | |
Fred Drake | 802a202 | 1998-02-19 06:26:35 +0000 | [diff] [blame] | 81 | The \module{UserList} module defines the \class{UserList} class: |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 82 | |
Fred Drake | 802a202 | 1998-02-19 06:26:35 +0000 | [diff] [blame] | 83 | \begin{classdesc}{UserList}{\optional{list}} |
Fred Drake | 2c4f554 | 2000-10-10 22:00:03 +0000 | [diff] [blame] | 84 | Class that simulates a list. The instance's |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 85 | contents are kept in a regular list, which is accessible via the |
Fred Drake | 802a202 | 1998-02-19 06:26:35 +0000 | [diff] [blame] | 86 | \member{data} attribute of \class{UserList} instances. The instance's |
Fred Drake | fcda560 | 1998-01-07 22:05:25 +0000 | [diff] [blame] | 87 | contents are initially set to a copy of \var{list}, defaulting to the |
Guido van Rossum | 7f3b042 | 1997-03-27 14:56:18 +0000 | [diff] [blame] | 88 | empty list \code{[]}. \var{list} can be either a regular Python list, |
Fred Drake | 802a202 | 1998-02-19 06:26:35 +0000 | [diff] [blame] | 89 | or an instance of \class{UserList} (or a subclass). |
| 90 | \end{classdesc} |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 91 | |
Fred Drake | 6bf37de | 1999-06-29 18:13:37 +0000 | [diff] [blame] | 92 | In addition to supporting the methods and operations of mutable |
| 93 | sequences (see section \ref{typesseq}), \class{UserList} instances |
| 94 | provide the following attribute: |
| 95 | |
Fred Drake | 1ce3604 | 1998-04-07 20:05:33 +0000 | [diff] [blame] | 96 | \begin{memberdesc}{data} |
| 97 | A real Python list object used to store the contents of the |
| 98 | \class{UserList} class. |
| 99 | \end{memberdesc} |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 100 | |
Fred Drake | c6243e4 | 2000-10-06 20:04:48 +0000 | [diff] [blame] | 101 | \strong{Subclassing requirements:} |
| 102 | Subclasses of \class{UserList} are expect to offer a constructor which |
| 103 | can be called with either no arguments or one argument. List |
| 104 | operations which return a new sequence attempt to create an instance |
| 105 | of the actual implementation class. To do so, it assumes that the |
| 106 | constructor can be called with a single parameter, which is a sequence |
| 107 | object used as a data source. |
| 108 | |
| 109 | If a derived class does not wish to comply with this requirement, all |
| 110 | of the special methods supported by this class will need to be |
| 111 | overridden; please consult the sources for information about the |
| 112 | methods 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 Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 120 | |
| 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 Drake | d5be3b7 | 2001-10-26 18:37:27 +0000 | [diff] [blame] | 129 | \note{This \class{UserString} class from this module is available for |
| 130 | backward compatibility only. If you are writing code that does not |
| 131 | need to work with versions of Python earlier than Python 2.2, please |
| 132 | consider subclassing directly from the built-in \class{str} type |
| 133 | instead of using \class{UserString} (there is no built-in equivalent |
| 134 | to \class{MutableString}).} |
| 135 | |
Fred Drake | 66c9f07 | 2000-10-10 20:58:48 +0000 | [diff] [blame] | 136 | This module defines a class that acts as a wrapper around string |
| 137 | objects. It is a useful base class for your own string-like classes, |
| 138 | which can inherit from them and override existing methods or add new |
| 139 | ones. In this way one can add new behaviors to strings. |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 140 | |
Fred Drake | 66c9f07 | 2000-10-10 20:58:48 +0000 | [diff] [blame] | 141 | It should be noted that these classes are highly inefficient compared |
| 142 | to real string or Unicode objects; this is especially the case for |
| 143 | \class{MutableString}. |
| 144 | |
| 145 | The \module{UserString} module defines the following classes: |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 146 | |
| 147 | \begin{classdesc}{UserString}{\optional{sequence}} |
Fred Drake | 2c4f554 | 2000-10-10 22:00:03 +0000 | [diff] [blame] | 148 | Class that simulates a string or a Unicode string |
Fred Drake | 66c9f07 | 2000-10-10 20:58:48 +0000 | [diff] [blame] | 149 | object. The instance's content is kept in a regular string or Unicode |
| 150 | string object, which is accessible via the \member{data} attribute of |
| 151 | \class{UserString} instances. The instance's contents are initially |
| 152 | set to a copy of \var{sequence}. \var{sequence} can be either a |
| 153 | regular Python string or Unicode string, an instance of |
| 154 | \class{UserString} (or a subclass) or an arbitrary sequence which can |
| 155 | be converted into a string using the built-in \function{str()} function. |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 156 | \end{classdesc} |
| 157 | |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 158 | \begin{classdesc}{MutableString}{\optional{sequence}} |
| 159 | This class is derived from the \class{UserString} above and redefines |
| 160 | strings to be \emph{mutable}. Mutable strings can't be used as |
| 161 | dictionary keys, because dictionaries require \emph{immutable} objects as |
| 162 | keys. The main intention of this class is to serve as an educational |
| 163 | example for inheritance and necessity to remove (override) the |
Fred Drake | 66c9f07 | 2000-10-10 20:58:48 +0000 | [diff] [blame] | 164 | \method{__hash__()} method in order to trap attempts to use a |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 165 | mutable object as dictionary key, which would be otherwise very |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 166 | error prone and hard to track down. |
Fred Drake | a22b576 | 2000-04-03 03:51:50 +0000 | [diff] [blame] | 167 | \end{classdesc} |
Fred Drake | 621d2be | 2000-09-09 03:23:50 +0000 | [diff] [blame] | 168 | |
Fred Drake | 66c9f07 | 2000-10-10 20:58:48 +0000 | [diff] [blame] | 169 | In addition to supporting the methods and operations of string and |
| 170 | Unicode objects (see section \ref{string-methods}, ``String |
| 171 | Methods''), \class{UserString} instances provide the following |
| 172 | attribute: |
Fred Drake | 621d2be | 2000-09-09 03:23:50 +0000 | [diff] [blame] | 173 | |
| 174 | \begin{memberdesc}{data} |
| 175 | A real Python string or Unicode object used to store the content of the |
| 176 | \class{UserString} class. |
| 177 | \end{memberdesc} |