/Doc/library/weakref.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 338 lines · 231 code · 107 blank · 0 comment · 0 complexity · 3fa5f448c1f4ae80defc4d969653b0b9 MD5 · raw file

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