Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`weakref` --- Weak references |
| 3 | ================================== |
| 4 | |
| 5 | .. module:: weakref |
| 6 | :synopsis: Support for weak references and weak dictionaries. |
| 7 | .. moduleauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | .. moduleauthor:: Neil Schemenauer <nas@arctrix.com> |
| 9 | .. moduleauthor:: Martin von Löwis <martin@loewis.home.cs.tu-berlin.de> |
| 10 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 11 | |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | The :mod:`weakref` module allows the Python programmer to create :dfn:`weak |
| 14 | references` to objects. |
| 15 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 16 | .. When making changes to the examples in this file, be sure to update |
| 17 | Lib/test/test_weakref.py::libreftest too! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
| 19 | In the following, the term :dfn:`referent` means the object which is referred to |
| 20 | by a weak reference. |
| 21 | |
| 22 | A weak reference to an object is not enough to keep the object alive: when the |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 23 | only remaining references to a referent are weak references, |
| 24 | :term:`garbage collection` is free to destroy the referent and reuse its memory |
| 25 | for something else. A primary use for weak references is to implement caches or |
| 26 | mappings holding large objects, where it's desired that a large object not be |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 27 | kept alive solely because it appears in a cache or mapping. |
| 28 | |
| 29 | For example, if you have a number of large binary image objects, you may wish to |
| 30 | associate a name with each. If you used a Python dictionary to map names to |
| 31 | images, or images to names, the image objects would remain alive just because |
| 32 | they appeared as values or keys in the dictionaries. The |
| 33 | :class:`WeakKeyDictionary` and :class:`WeakValueDictionary` classes supplied by |
| 34 | the :mod:`weakref` module are an alternative, using weak references to construct |
| 35 | mappings that don't keep objects alive solely because they appear in the mapping |
| 36 | objects. If, for example, an image object is a value in a |
| 37 | :class:`WeakValueDictionary`, then when the last remaining references to that |
| 38 | image object are the weak references held by weak mappings, garbage collection |
| 39 | can reclaim the object, and its corresponding entries in weak mappings are |
| 40 | simply deleted. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 42 | :class:`WeakKeyDictionary` and :class:`WeakValueDictionary` use weak references |
| 43 | in their implementation, setting up callback functions on the weak references |
| 44 | that notify the weak dictionaries when a key or value has been reclaimed by |
Georg Brandl | 3b8cb17 | 2007-10-23 06:26:46 +0000 | [diff] [blame] | 45 | garbage collection. :class:`WeakSet` implements the :class:`set` interface, |
| 46 | but keeps weak references to its elements, just like a |
| 47 | :class:`WeakKeyDictionary` does. |
| 48 | |
| 49 | Most programs should find that using one of these weak container types is all |
| 50 | they need -- it's not usually necessary to create your own weak references |
| 51 | directly. The low-level machinery used by the weak dictionary implementations |
| 52 | is exposed by the :mod:`weakref` module for the benefit of advanced uses. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 54 | .. note:: |
| 55 | |
| 56 | Weak references to an object are cleared before the object's :meth:`__del__` |
| 57 | is called, to ensure that the weak reference callback (if any) finds the |
| 58 | object still alive. |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | Not all objects can be weakly referenced; those objects which can include class |
Georg Brandl | 2e0b755 | 2007-11-27 12:43:08 +0000 | [diff] [blame] | 61 | instances, functions written in Python (but not in C), instance methods, sets, |
| 62 | frozensets, file objects, :term:`generator`\s, type objects, :class:`DBcursor` |
| 63 | objects from the :mod:`bsddb` module, sockets, arrays, deques, and regular |
| 64 | expression pattern objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | Several builtin types such as :class:`list` and :class:`dict` do not directly |
| 67 | support weak references but can add support through subclassing:: |
| 68 | |
| 69 | class Dict(dict): |
| 70 | pass |
| 71 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 72 | obj = Dict(red=1, green=2, blue=3) # this object is weak referenceable |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | Extension types can easily be made to support weak references; see |
| 75 | :ref:`weakref-support`. |
| 76 | |
| 77 | |
| 78 | .. class:: ref(object[, callback]) |
| 79 | |
| 80 | Return a weak reference to *object*. The original object can be retrieved by |
| 81 | calling the reference object if the referent is still alive; if the referent is |
| 82 | no longer alive, calling the reference object will cause :const:`None` to be |
| 83 | returned. If *callback* is provided and not :const:`None`, and the returned |
| 84 | weakref object is still alive, the callback will be called when the object is |
| 85 | about to be finalized; the weak reference object will be passed as the only |
| 86 | parameter to the callback; the referent will no longer be available. |
| 87 | |
| 88 | It is allowable for many weak references to be constructed for the same object. |
| 89 | Callbacks registered for each weak reference will be called from the most |
| 90 | recently registered callback to the oldest registered callback. |
| 91 | |
| 92 | Exceptions raised by the callback will be noted on the standard error output, |
| 93 | but cannot be propagated; they are handled in exactly the same way as exceptions |
| 94 | raised from an object's :meth:`__del__` method. |
| 95 | |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 96 | Weak references are :term:`hashable` if the *object* is hashable. They will maintain |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | their hash value even after the *object* was deleted. If :func:`hash` is called |
| 98 | the first time only after the *object* was deleted, the call will raise |
| 99 | :exc:`TypeError`. |
| 100 | |
| 101 | Weak references support tests for equality, but not ordering. If the referents |
| 102 | are still alive, two references have the same equality relationship as their |
| 103 | referents (regardless of the *callback*). If either referent has been deleted, |
| 104 | the references are equal only if the reference objects are the same object. |
| 105 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 106 | This is a subclassable type rather than a factory function. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | .. function:: proxy(object[, callback]) |
| 110 | |
| 111 | Return a proxy to *object* which uses a weak reference. This supports use of |
| 112 | the proxy in most contexts instead of requiring the explicit dereferencing used |
| 113 | with weak reference objects. The returned object will have a type of either |
| 114 | ``ProxyType`` or ``CallableProxyType``, depending on whether *object* is |
Guido van Rossum | 2cc30da | 2007-11-02 23:46:40 +0000 | [diff] [blame] | 115 | callable. Proxy objects are not :term:`hashable` regardless of the referent; this |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | avoids a number of problems related to their fundamentally mutable nature, and |
| 117 | prevent their use as dictionary keys. *callback* is the same as the parameter |
| 118 | of the same name to the :func:`ref` function. |
| 119 | |
| 120 | |
| 121 | .. function:: getweakrefcount(object) |
| 122 | |
| 123 | Return the number of weak references and proxies which refer to *object*. |
| 124 | |
| 125 | |
| 126 | .. function:: getweakrefs(object) |
| 127 | |
| 128 | Return a list of all weak reference and proxy objects which refer to *object*. |
| 129 | |
| 130 | |
| 131 | .. class:: WeakKeyDictionary([dict]) |
| 132 | |
| 133 | Mapping class that references keys weakly. Entries in the dictionary will be |
| 134 | discarded when there is no longer a strong reference to the key. This can be |
| 135 | used to associate additional data with an object owned by other parts of an |
| 136 | application without adding attributes to those objects. This can be especially |
| 137 | useful with objects that override attribute accesses. |
| 138 | |
| 139 | .. note:: |
| 140 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 141 | Caution: Because a :class:`WeakKeyDictionary` is built on top of a Python |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 | dictionary, it must not change size when iterating over it. This can be |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 143 | difficult to ensure for a :class:`WeakKeyDictionary` because actions |
| 144 | performed by the program during iteration may cause items in the |
| 145 | dictionary to vanish "by magic" (as a side effect of garbage collection). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | :class:`WeakKeyDictionary` objects have the following additional methods. These |
| 148 | expose the internal references directly. The references are not guaranteed to |
| 149 | be "live" at the time they are used, so the result of calling the references |
| 150 | needs to be checked before being used. This can be used to avoid creating |
| 151 | references that will cause the garbage collector to keep the keys around longer |
| 152 | than needed. |
| 153 | |
| 154 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 155 | .. method:: WeakKeyDictionary.keyrefs() |
| 156 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 157 | Return an :term:`iterator` that yields the weak references to the keys. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | |
| 160 | .. class:: WeakValueDictionary([dict]) |
| 161 | |
| 162 | Mapping class that references values weakly. Entries in the dictionary will be |
| 163 | discarded when no strong reference to the value exists any more. |
| 164 | |
| 165 | .. note:: |
| 166 | |
| 167 | Caution: Because a :class:`WeakValueDictionary` is built on top of a Python |
| 168 | dictionary, it must not change size when iterating over it. This can be |
| 169 | difficult to ensure for a :class:`WeakValueDictionary` because actions performed |
| 170 | by the program during iteration may cause items in the dictionary to vanish "by |
| 171 | magic" (as a side effect of garbage collection). |
| 172 | |
| 173 | :class:`WeakValueDictionary` objects have the following additional methods. |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 174 | These method have the same issues as the and :meth:`keyrefs` method of |
| 175 | :class:`WeakKeyDictionary` objects. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 177 | |
| 178 | .. method:: WeakValueDictionary.valuerefs() |
| 179 | |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 180 | Return an :term:`iterator` that yields the weak references to the values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
Georg Brandl | 3b8cb17 | 2007-10-23 06:26:46 +0000 | [diff] [blame] | 183 | .. class:: WeakSet([elements]) |
| 184 | |
| 185 | Set class that keeps weak references to its elements. An element will be |
| 186 | discarded when no strong reference to it exists any more. |
| 187 | |
| 188 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | .. data:: ReferenceType |
| 190 | |
| 191 | The type object for weak references objects. |
| 192 | |
| 193 | |
| 194 | .. data:: ProxyType |
| 195 | |
| 196 | The type object for proxies of objects which are not callable. |
| 197 | |
| 198 | |
| 199 | .. data:: CallableProxyType |
| 200 | |
| 201 | The type object for proxies of callable objects. |
| 202 | |
| 203 | |
| 204 | .. data:: ProxyTypes |
| 205 | |
| 206 | Sequence containing all the type objects for proxies. This can make it simpler |
| 207 | to test if an object is a proxy without being dependent on naming both proxy |
| 208 | types. |
| 209 | |
| 210 | |
| 211 | .. exception:: ReferenceError |
| 212 | |
| 213 | Exception raised when a proxy object is used but the underlying object has been |
| 214 | collected. This is the same as the standard :exc:`ReferenceError` exception. |
| 215 | |
| 216 | |
| 217 | .. seealso:: |
| 218 | |
| 219 | :pep:`0205` - Weak References |
| 220 | The proposal and rationale for this feature, including links to earlier |
| 221 | implementations and information about similar features in other languages. |
| 222 | |
| 223 | |
| 224 | .. _weakref-objects: |
| 225 | |
| 226 | Weak Reference Objects |
| 227 | ---------------------- |
| 228 | |
| 229 | Weak reference objects have no attributes or methods, but do allow the referent |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 230 | to be obtained, if it still exists, by calling it: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 231 | |
| 232 | >>> import weakref |
| 233 | >>> class Object: |
| 234 | ... pass |
| 235 | ... |
| 236 | >>> o = Object() |
| 237 | >>> r = weakref.ref(o) |
| 238 | >>> o2 = r() |
| 239 | >>> o is o2 |
| 240 | True |
| 241 | |
| 242 | If the referent no longer exists, calling the reference object returns |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 243 | :const:`None`: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 244 | |
| 245 | >>> del o, o2 |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 246 | >>> print(r()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 247 | None |
| 248 | |
| 249 | Testing that a weak reference object is still live should be done using the |
| 250 | expression ``ref() is not None``. Normally, application code that needs to use |
| 251 | a reference object should follow this pattern:: |
| 252 | |
| 253 | # r is a weak reference object |
| 254 | o = r() |
| 255 | if o is None: |
| 256 | # referent has been garbage collected |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 257 | print("Object has been deallocated; can't frobnicate.") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 258 | else: |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 259 | print("Object is still live!") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | o.do_something_useful() |
| 261 | |
| 262 | Using a separate test for "liveness" creates race conditions in threaded |
| 263 | applications; another thread can cause a weak reference to become invalidated |
| 264 | before the weak reference is called; the idiom shown above is safe in threaded |
| 265 | applications as well as single-threaded applications. |
| 266 | |
| 267 | Specialized versions of :class:`ref` objects can be created through subclassing. |
| 268 | This is used in the implementation of the :class:`WeakValueDictionary` to reduce |
| 269 | the memory overhead for each entry in the mapping. This may be most useful to |
| 270 | associate additional information with a reference, but could also be used to |
| 271 | insert additional processing on calls to retrieve the referent. |
| 272 | |
| 273 | This example shows how a subclass of :class:`ref` can be used to store |
| 274 | additional information about an object and affect the value that's returned when |
| 275 | the referent is accessed:: |
| 276 | |
| 277 | import weakref |
| 278 | |
| 279 | class ExtendedRef(weakref.ref): |
| 280 | def __init__(self, ob, callback=None, **annotations): |
| 281 | super(ExtendedRef, self).__init__(ob, callback) |
| 282 | self.__counter = 0 |
Barry Warsaw | ecaab83 | 2008-09-04 01:42:51 +0000 | [diff] [blame] | 283 | for k, v in annotations.items(): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 284 | setattr(self, k, v) |
| 285 | |
| 286 | def __call__(self): |
| 287 | """Return a pair containing the referent and the number of |
| 288 | times the reference has been called. |
| 289 | """ |
| 290 | ob = super(ExtendedRef, self).__call__() |
| 291 | if ob is not None: |
| 292 | self.__counter += 1 |
| 293 | ob = (ob, self.__counter) |
| 294 | return ob |
| 295 | |
| 296 | |
| 297 | .. _weakref-example: |
| 298 | |
| 299 | Example |
| 300 | ------- |
| 301 | |
| 302 | This simple example shows how an application can use objects IDs to retrieve |
| 303 | objects that it has seen before. The IDs of the objects can then be used in |
| 304 | other data structures without forcing the objects to remain alive, but the |
| 305 | objects can still be retrieved by ID if they do. |
| 306 | |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 307 | .. Example contributed by Tim Peters. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 308 | |
| 309 | :: |
| 310 | |
| 311 | import weakref |
| 312 | |
| 313 | _id2obj_dict = weakref.WeakValueDictionary() |
| 314 | |
| 315 | def remember(obj): |
| 316 | oid = id(obj) |
| 317 | _id2obj_dict[oid] = obj |
| 318 | return oid |
| 319 | |
| 320 | def id2obj(oid): |
| 321 | return _id2obj_dict[oid] |
| 322 | |