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-fpectl.html" /> | |
13 | <link rel="prev" href="module-gc.html" /> | |
14 | <link rel="parent" href="python.html" /> | |
15 | <link rel="next" href="weakref-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.3 weakref -- Weak references</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.2 gc " | |
25 | href="module-gc.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.3.1 Weak Reference Objects" | |
31 | href="weakref-objects.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-gc.html">3.2 gc </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="weakref-objects.html">3.3.1 Weak Reference Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION005300000000000000000"> | |
56 | 3.3 <tt class="module">weakref</tt> -- | |
57 | Weak references</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-weakref"></A> | |
62 | ||
63 | <P> | |
64 | ||
65 | <span class="versionnote">New in version 2.1.</span> | |
66 | ||
67 | <P> | |
68 | The <tt class="module">weakref</tt> module allows the Python programmer to create | |
69 | <i class="dfn">weak references</i> to objects. | |
70 | ||
71 | <P> | |
72 | In the following, the term <i class="dfn">referent</i> means the | |
73 | object which is referred to by a weak reference. | |
74 | ||
75 | <P> | |
76 | A weak reference to an object is not enough to keep the object alive: | |
77 | when the only remaining references to a referent are weak references, | |
78 | garbage collection is free to destroy the referent and reuse its memory | |
79 | for something else. A primary use for weak references is to implement | |
80 | caches or mappings holding large objects, where it's desired that a | |
81 | large object not be kept alive solely because it appears in a cache or | |
82 | mapping. For example, if you have a number of large binary image objects, | |
83 | you may wish to associate a name with each. If you used a Python | |
84 | dictionary to map names to images, or images to names, the image objects | |
85 | would remain alive just because they appeared as values or keys in the | |
86 | dictionaries. The <tt class="class">WeakKeyDictionary</tt> and | |
87 | <tt class="class">WeakValueDictionary</tt> classes supplied by the <tt class="module">weakref</tt> | |
88 | module are an alternative, using weak references to construct mappings | |
89 | that don't keep objects alive solely because they appear in the mapping | |
90 | objects. If, for example, an image object is a value in a | |
91 | <tt class="class">WeakValueDictionary</tt>, then when the last remaining | |
92 | references to that image object are the weak references held by weak | |
93 | mappings, garbage collection can reclaim the object, and its corresponding | |
94 | entries in weak mappings are simply deleted. | |
95 | ||
96 | <P> | |
97 | <tt class="class">WeakKeyDictionary</tt> and <tt class="class">WeakValueDictionary</tt> use weak | |
98 | references in their implementation, setting up callback functions on | |
99 | the weak references that notify the weak dictionaries when a key or value | |
100 | has been reclaimed by garbage collection. Most programs should find that | |
101 | using one of these weak dictionary types is all they need - it's | |
102 | not usually necessary to create your own weak references directly. The | |
103 | low-level machinery used by the weak dictionary implementations is exposed | |
104 | by the <tt class="module">weakref</tt> module for the benefit of advanced uses. | |
105 | ||
106 | <P> | |
107 | Not all objects can be weakly referenced; those objects which can | |
108 | include class instances, functions written in Python (but not in C), | |
109 | methods (both bound and unbound), sets, frozensets, file objects, | |
110 | generators, type objects, DBcursor objects from the <tt class="module">bsddb</tt> module, | |
111 | sockets, arrays, deques, and regular expression pattern objects. | |
112 | ||
113 | <span class="versionnote">Changed in version 2.4: | |
114 | Added support for files, sockets, arrays, and patterns.</span> | |
115 | ||
116 | <P> | |
117 | Several builtin types such as <tt class="class">list</tt> and <tt class="class">dict</tt> do not | |
118 | directly support weak references but can add support through subclassing: | |
119 | ||
120 | <P> | |
121 | <div class="verbatim"><pre> | |
122 | class Dict(dict): | |
123 | pass | |
124 | ||
125 | obj = Dict(red=1, green=2, blue=3) # this object is weak referencable | |
126 | </pre></div> | |
127 | ||
128 | <P> | |
129 | Extension types can easily be made to support weak references; see section | |
130 | <A href="weakref-extension.html#weakref-extension">3.3.3</A>, ``Weak References in Extension Types,'' for more | |
131 | information. | |
132 | ||
133 | <P> | |
134 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
135 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-419' xml:id='l2h-419' class="class">ref</tt></b>(</nobr></td> | |
136 | <td><var>object</var><big>[</big><var>, callback</var><big>]</big><var></var>)</td></tr></table></dt> | |
137 | <dd> | |
138 | Return a weak reference to <var>object</var>. The original object can be | |
139 | retrieved by calling the reference object if the referent is still | |
140 | alive; if the referent is no longer alive, calling the reference | |
141 | object will cause <tt class="constant">None</tt> to be returned. If <var>callback</var> is | |
142 | provided and not <tt class="constant">None</tt>, | |
143 | it will be called when the object is about to be | |
144 | finalized; the weak reference object will be passed as the only | |
145 | parameter to the callback; the referent will no longer be available. | |
146 | ||
147 | <P> | |
148 | It is allowable for many weak references to be constructed for the | |
149 | same object. Callbacks registered for each weak reference will be | |
150 | called from the most recently registered callback to the oldest | |
151 | registered callback. | |
152 | ||
153 | <P> | |
154 | Exceptions raised by the callback will be noted on the standard | |
155 | error output, but cannot be propagated; they are handled in exactly | |
156 | the same way as exceptions raised from an object's | |
157 | <tt class="method">__del__()</tt> method. | |
158 | ||
159 | <P> | |
160 | Weak references are hashable if the <var>object</var> is hashable. They | |
161 | will maintain their hash value even after the <var>object</var> was | |
162 | deleted. If <tt class="function">hash()</tt> is called the first time only after | |
163 | the <var>object</var> was deleted, the call will raise | |
164 | <tt class="exception">TypeError</tt>. | |
165 | ||
166 | <P> | |
167 | Weak references support tests for equality, but not ordering. If | |
168 | the referents are still alive, two references have the same | |
169 | equality relationship as their referents (regardless of the | |
170 | <var>callback</var>). If either referent has been deleted, the | |
171 | references are equal only if the reference objects are the same | |
172 | object. | |
173 | ||
174 | <P> | |
175 | ||
176 | <span class="versionnote">Changed in version 2.4: | |
177 | This is now a subclassable type rather than a | |
178 | factory function; it derives from <tt class="class">object</tt>.</span> | |
179 | ||
180 | </dl> | |
181 | ||
182 | <P> | |
183 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
184 | <td><nobr><b><tt id='l2h-420' xml:id='l2h-420' class="function">proxy</tt></b>(</nobr></td> | |
185 | <td><var>object</var><big>[</big><var>, callback</var><big>]</big><var></var>)</td></tr></table></dt> | |
186 | <dd> | |
187 | Return a proxy to <var>object</var> which uses a weak reference. This | |
188 | supports use of the proxy in most contexts instead of requiring the | |
189 | explicit dereferencing used with weak reference objects. The | |
190 | returned object will have a type of either <code>ProxyType</code> or | |
191 | <code>CallableProxyType</code>, depending on whether <var>object</var> is | |
192 | callable. Proxy objects are not hashable regardless of the | |
193 | referent; this avoids a number of problems related to their | |
194 | fundamentally mutable nature, and prevent their use as dictionary | |
195 | keys. <var>callback</var> is the same as the parameter of the same name | |
196 | to the <tt class="function">ref()</tt> function. | |
197 | </dl> | |
198 | ||
199 | <P> | |
200 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
201 | <td><nobr><b><tt id='l2h-421' xml:id='l2h-421' class="function">getweakrefcount</tt></b>(</nobr></td> | |
202 | <td><var>object</var>)</td></tr></table></dt> | |
203 | <dd> | |
204 | Return the number of weak references and proxies which refer to | |
205 | <var>object</var>. | |
206 | </dl> | |
207 | ||
208 | <P> | |
209 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
210 | <td><nobr><b><tt id='l2h-422' xml:id='l2h-422' class="function">getweakrefs</tt></b>(</nobr></td> | |
211 | <td><var>object</var>)</td></tr></table></dt> | |
212 | <dd> | |
213 | Return a list of all weak reference and proxy objects which refer to | |
214 | <var>object</var>. | |
215 | </dl> | |
216 | ||
217 | <P> | |
218 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
219 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-423' xml:id='l2h-423' class="class">WeakKeyDictionary</tt></b>(</nobr></td> | |
220 | <td><var></var><big>[</big><var>dict</var><big>]</big><var></var>)</td></tr></table></dt> | |
221 | <dd> | |
222 | Mapping class that references keys weakly. Entries in the | |
223 | dictionary will be discarded when there is no longer a strong | |
224 | reference to the key. This can be used to associate additional data | |
225 | with an object owned by other parts of an application without adding | |
226 | attributes to those objects. This can be especially useful with | |
227 | objects that override attribute accesses. | |
228 | ||
229 | <P> | |
230 | <span class="note"><b class="label">Note:</b> | |
231 | Caution: Because a <tt class="class">WeakKeyDictionary</tt> is built on top | |
232 | of a Python dictionary, it must not change size when iterating | |
233 | over it. This can be difficult to ensure for a | |
234 | <tt class="class">WeakKeyDictionary</tt> because actions performed by the | |
235 | program during iteration may cause items in the dictionary | |
236 | to vanish "by magic" (as a side effect of garbage collection).</span> | |
237 | </dl> | |
238 | ||
239 | <P> | |
240 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
241 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-424' xml:id='l2h-424' class="class">WeakValueDictionary</tt></b>(</nobr></td> | |
242 | <td><var></var><big>[</big><var>dict</var><big>]</big><var></var>)</td></tr></table></dt> | |
243 | <dd> | |
244 | Mapping class that references values weakly. Entries in the | |
245 | dictionary will be discarded when no strong reference to the value | |
246 | exists any more. | |
247 | ||
248 | <P> | |
249 | <span class="note"><b class="label">Note:</b> | |
250 | Caution: Because a <tt class="class">WeakValueDictionary</tt> is built on top | |
251 | of a Python dictionary, it must not change size when iterating | |
252 | over it. This can be difficult to ensure for a | |
253 | <tt class="class">WeakValueDictionary</tt> because actions performed by the | |
254 | program during iteration may cause items in the dictionary | |
255 | to vanish "by magic" (as a side effect of garbage collection).</span> | |
256 | </dl> | |
257 | ||
258 | <P> | |
259 | <dl><dt><b><tt id='l2h-425' xml:id='l2h-425'>ReferenceType</tt></b></dt> | |
260 | <dd> | |
261 | The type object for weak references objects. | |
262 | </dd></dl> | |
263 | ||
264 | <P> | |
265 | <dl><dt><b><tt id='l2h-426' xml:id='l2h-426'>ProxyType</tt></b></dt> | |
266 | <dd> | |
267 | The type object for proxies of objects which are not callable. | |
268 | </dd></dl> | |
269 | ||
270 | <P> | |
271 | <dl><dt><b><tt id='l2h-427' xml:id='l2h-427'>CallableProxyType</tt></b></dt> | |
272 | <dd> | |
273 | The type object for proxies of callable objects. | |
274 | </dd></dl> | |
275 | ||
276 | <P> | |
277 | <dl><dt><b><tt id='l2h-428' xml:id='l2h-428'>ProxyTypes</tt></b></dt> | |
278 | <dd> | |
279 | Sequence containing all the type objects for proxies. This can make | |
280 | it simpler to test if an object is a proxy without being dependent | |
281 | on naming both proxy types. | |
282 | </dd></dl> | |
283 | ||
284 | <P> | |
285 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-429' xml:id='l2h-429' class="exception">ReferenceError</tt></b></dt> | |
286 | <dd> | |
287 | Exception raised when a proxy object is used but the underlying | |
288 | object has been collected. This is the same as the standard | |
289 | <tt class="exception">ReferenceError</tt> exception. | |
290 | </dd></dl> | |
291 | ||
292 | <P> | |
293 | <div class="seealso"> | |
294 | <p class="heading">See Also:</p> | |
295 | ||
296 | <dl compact="compact" class="seerfc"> | |
297 | <dt><a href="http://www.python.org/peps/pep-0205.html" | |
298 | title="Weak References" | |
299 | >PEP 0205, <em>Weak References</em></a> | |
300 | <dd>The proposal and rationale for this | |
301 | feature, including links to earlier implementations | |
302 | and information about similar features in other | |
303 | languages. | |
304 | </dl> | |
305 | </div> | |
306 | ||
307 | <P> | |
308 | ||
309 | <p><br /></p><hr class='online-navigation' /> | |
310 | <div class='online-navigation'> | |
311 | <!--Table of Child-Links--> | |
312 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
313 | ||
314 | <UL CLASS="ChildLinks"> | |
315 | <LI><A href="weakref-objects.html">3.3.1 Weak Reference Objects</a> | |
316 | <LI><A href="weakref-example.html">3.3.2 Example</a> | |
317 | <LI><A href="weakref-extension.html">3.3.3 Weak References in Extension Types</a> | |
318 | </ul> | |
319 | <!--End of Table of Child-Links--> | |
320 | </div> | |
321 | ||
322 | <DIV CLASS="navigation"> | |
323 | <div class='online-navigation'> | |
324 | <p></p><hr /> | |
325 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
326 | <tr> | |
327 | <td class='online-navigation'><a rel="prev" title="3.2 gc " | |
328 | href="module-gc.html"><img src='../icons/previous.png' | |
329 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
330 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" | |
331 | href="python.html"><img src='../icons/up.png' | |
332 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
333 | <td class='online-navigation'><a rel="next" title="3.3.1 Weak Reference Objects" | |
334 | href="weakref-objects.html"><img src='../icons/next.png' | |
335 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
336 | <td align="center" width="100%">Python Library Reference</td> | |
337 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
338 | href="contents.html"><img src='../icons/contents.png' | |
339 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
340 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
341 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
342 | <td class='online-navigation'><a rel="index" title="Index" | |
343 | href="genindex.html"><img src='../icons/index.png' | |
344 | border='0' height='32' alt='Index' width='32' /></A></td> | |
345 | </tr></table> | |
346 | <div class='online-navigation'> | |
347 | <b class="navlabel">Previous:</b> | |
348 | <a class="sectref" rel="prev" href="module-gc.html">3.2 gc </A> | |
349 | <b class="navlabel">Up:</b> | |
350 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> | |
351 | <b class="navlabel">Next:</b> | |
352 | <a class="sectref" rel="next" href="weakref-objects.html">3.3.1 Weak Reference Objects</A> | |
353 | </div> | |
354 | </div> | |
355 | <hr /> | |
356 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
357 | </DIV> | |
358 | <!--End of Navigation Panel--> | |
359 | <ADDRESS> | |
360 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
361 | </ADDRESS> | |
362 | </BODY> | |
363 | </HTML> |