Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-UserList.html" /> | |
13 | <link rel="prev" href="module-types.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="module-UserList.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.7 UserDict -- Class wrapper for dictionary objects</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.6 types " | |
25 | href="module-types.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
28 | href="python.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.8 UserList " | |
31 | href="module-UserList.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-types.html">3.6 types </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="module-UserList.html">3.8 UserList </A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION005700000000000000000"> | |
56 | 3.7 <tt class="module">UserDict</tt> -- | |
57 | Class wrapper for dictionary objects</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-UserDict"></A> | |
62 | ||
63 | <P> | |
64 | The module defines a mixin, <tt class="class">DictMixin</tt>, defining all dictionary | |
65 | methods for classes that already have a minimum mapping interface. This | |
66 | greatly simplifies writing classes that need to be substitutable for | |
67 | dictionaries (such as the shelve module). | |
68 | ||
69 | <P> | |
70 | This also module defines a class, <tt class="class">UserDict</tt>, that acts as a wrapper | |
71 | around dictionary objects. The need for this class has been largely | |
72 | supplanted by the ability to subclass directly from <tt class="class">dict</tt> (a feature | |
73 | that became available starting with Python version 2.2). Prior to the | |
74 | introduction of <tt class="class">dict</tt>, the <tt class="class">UserDict</tt> class was used to | |
75 | create dictionary-like sub-classes that obtained new behaviors by overriding | |
76 | existing methods or adding new ones. | |
77 | ||
78 | <P> | |
79 | The <tt class="module">UserDict</tt> module defines the <tt class="class">UserDict</tt> class | |
80 | and <tt class="class">DictMixin</tt>: | |
81 | ||
82 | <P> | |
83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
84 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-478' xml:id='l2h-478' class="class">UserDict</tt></b>(</nobr></td> | |
85 | <td><var></var><big>[</big><var>initialdata</var><big>]</big><var></var>)</td></tr></table></dt> | |
86 | <dd> | |
87 | Class that simulates a dictionary. The instance's | |
88 | contents are kept in a regular dictionary, which is accessible via the | |
89 | <tt class="member">data</tt> attribute of <tt class="class">UserDict</tt> instances. If | |
90 | <var>initialdata</var> is provided, <tt class="member">data</tt> is initialized with its | |
91 | contents; note that a reference to <var>initialdata</var> will not be kept, | |
92 | allowing it be used for other purposes. <span class="note"><b class="label">Note:</b> | |
93 | For backward compatibility, | |
94 | instances of <tt class="class">UserDict</tt> are not iterable.</span> | |
95 | </dl> | |
96 | ||
97 | <P> | |
98 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
99 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-479' xml:id='l2h-479' class="class">IterableUserDict</tt></b>(</nobr></td> | |
100 | <td><var></var><big>[</big><var>initialdata</var><big>]</big><var></var>)</td></tr></table></dt> | |
101 | <dd> | |
102 | Subclass of <tt class="class">UserDict</tt> that supports direct iteration (e.g. | |
103 | <code>for key in myDict</code>). | |
104 | </dl> | |
105 | ||
106 | <P> | |
107 | In addition to supporting the methods and operations of mappings (see | |
108 | section <A href="typesmapping.html#typesmapping">2.3.8</A>), <tt class="class">UserDict</tt> and | |
109 | <tt class="class">IterableUserDict</tt> instances provide the following attribute: | |
110 | ||
111 | <P> | |
112 | <dl><dt><b><tt id='l2h-480' xml:id='l2h-480' class="member">data</tt></b></dt> | |
113 | <dd> | |
114 | A real dictionary used to store the contents of the <tt class="class">UserDict</tt> | |
115 | class. | |
116 | </dl> | |
117 | ||
118 | <P> | |
119 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
120 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-481' xml:id='l2h-481' class="class">DictMixin</tt></b>(</nobr></td> | |
121 | <td><var></var>)</td></tr></table></dt> | |
122 | <dd> | |
123 | Mixin defining all dictionary methods for classes that already have | |
124 | a minimum dictionary interface including <tt class="method">__getitem__()</tt>, | |
125 | <tt class="method">__setitem__()</tt>, <tt class="method">__delitem__()</tt>, and <tt class="method">keys()</tt>. | |
126 | ||
127 | <P> | |
128 | This mixin should be used as a superclass. Adding each of the | |
129 | above methods adds progressively more functionality. For instance, | |
130 | defining all but <tt class="method">__delitem__</tt> will preclude only <tt class="method">pop</tt> | |
131 | and <tt class="method">popitem</tt> from the full interface. | |
132 | ||
133 | <P> | |
134 | In addition to the four base methods, progressively more efficiency | |
135 | comes with defining <tt class="method">__contains__()</tt>, <tt class="method">__iter__()</tt>, and | |
136 | <tt class="method">iteritems()</tt>. | |
137 | ||
138 | <P> | |
139 | Since the mixin has no knowledge of the subclass constructor, it | |
140 | does not define <tt class="method">__init__()</tt> or <tt class="method">copy()</tt>. | |
141 | </dl> | |
142 | ||
143 | <P> | |
144 | ||
145 | <DIV CLASS="navigation"> | |
146 | <div class='online-navigation'> | |
147 | <p></p><hr /> | |
148 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
149 | <tr> | |
150 | <td class='online-navigation'><a rel="prev" title="3.6 types " | |
151 | href="module-types.html"><img src='../icons/previous.png' | |
152 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
153 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
154 | href="python.html"><img src='../icons/up.png' | |
155 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
156 | <td class='online-navigation'><a rel="next" title="3.8 UserList " | |
157 | href="module-UserList.html"><img src='../icons/next.png' | |
158 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
159 | <td align="center" width="100%">Python Library Reference</td> | |
160 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
161 | href="contents.html"><img src='../icons/contents.png' | |
162 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
163 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
164 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
165 | <td class='online-navigation'><a rel="index" title="Index" | |
166 | href="genindex.html"><img src='../icons/index.png' | |
167 | border='0' height='32' alt='Index' width='32' /></A></td> | |
168 | </tr></table> | |
169 | <div class='online-navigation'> | |
170 | <b class="navlabel">Previous:</b> | |
171 | <a class="sectref" rel="prev" href="module-types.html">3.6 types </A> | |
172 | <b class="navlabel">Up:</b> | |
173 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
174 | <b class="navlabel">Next:</b> | |
175 | <a class="sectref" rel="next" href="module-UserList.html">3.8 UserList </A> | |
176 | </div> | |
177 | </div> | |
178 | <hr /> | |
179 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
180 | </DIV> | |
181 | <!--End of Navigation Panel--> | |
182 | <ADDRESS> | |
183 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
184 | </ADDRESS> | |
185 | </BODY> | |
186 | </HTML> |