| 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="weakref-example.html" /> |
| 13 | <link rel="prev" href="module-weakref.html" /> |
| 14 | <link rel="parent" href="module-weakref.html" /> |
| 15 | <link rel="next" href="weakref-example.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.3.1 Weak Reference 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.3 weakref " |
| 25 | href="module-weakref.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.3 weakref " |
| 28 | href="module-weakref.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.2 Example" |
| 31 | href="weakref-example.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-weakref.html">3.3 weakref </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-weakref.html">3.3 weakref </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="weakref-example.html">3.3.2 Example</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION005310000000000000000"></A><A NAME="weakref-objects"></A> |
| 56 | <BR> |
| 57 | 3.3.1 Weak Reference Objects |
| 58 | |
| 59 | </H2> |
| 60 | |
| 61 | <P> |
| 62 | Weak reference objects have no attributes or methods, but do allow the |
| 63 | referent to be obtained, if it still exists, by calling it: |
| 64 | |
| 65 | <P> |
| 66 | <div class="verbatim"><pre> |
| 67 | >>> import weakref |
| 68 | >>> class Object: |
| 69 | ... pass |
| 70 | ... |
| 71 | >>> o = Object() |
| 72 | >>> r = weakref.ref(o) |
| 73 | >>> o2 = r() |
| 74 | >>> o is o2 |
| 75 | True |
| 76 | </pre></div> |
| 77 | |
| 78 | <P> |
| 79 | If the referent no longer exists, calling the reference object returns |
| 80 | <tt class="constant">None</tt>: |
| 81 | |
| 82 | <P> |
| 83 | <div class="verbatim"><pre> |
| 84 | >>> del o, o2 |
| 85 | >>> print r() |
| 86 | None |
| 87 | </pre></div> |
| 88 | |
| 89 | <P> |
| 90 | Testing that a weak reference object is still live should be done |
| 91 | using the expression <code><var>ref</var>() is not None</code>. Normally, |
| 92 | application code that needs to use a reference object should follow |
| 93 | this pattern: |
| 94 | |
| 95 | <P> |
| 96 | <div class="verbatim"><pre> |
| 97 | # r is a weak reference object |
| 98 | o = r() |
| 99 | if o is None: |
| 100 | # referent has been garbage collected |
| 101 | print "Object has been deallocated; can't frobnicate." |
| 102 | else: |
| 103 | print "Object is still live!" |
| 104 | o.do_something_useful() |
| 105 | </pre></div> |
| 106 | |
| 107 | <P> |
| 108 | Using a separate test for ``liveness'' creates race conditions in |
| 109 | threaded applications; another thread can cause a weak reference to |
| 110 | become invalidated before the weak reference is called; the |
| 111 | idiom shown above is safe in threaded applications as well as |
| 112 | single-threaded applications. |
| 113 | |
| 114 | <P> |
| 115 | Specialized versions of <tt class="class">ref</tt> objects can be created through |
| 116 | subclassing. This is used in the implementation of the |
| 117 | <tt class="class">WeakValueDictionary</tt> to reduce the memory overhead for each |
| 118 | entry in the mapping. This may be most useful to associate additional |
| 119 | information with a reference, but could also be used to insert |
| 120 | additional processing on calls to retrieve the referent. |
| 121 | |
| 122 | <P> |
| 123 | This example shows how a subclass of <tt class="class">ref</tt> can be used to store |
| 124 | additional information about an object and affect the value that's |
| 125 | returned when the referent is accessed: |
| 126 | |
| 127 | <P> |
| 128 | <div class="verbatim"><pre> |
| 129 | import weakref |
| 130 | |
| 131 | class ExtendedRef(weakref.ref): |
| 132 | def __init__(self, ob, callback=None, **annotations): |
| 133 | super(ExtendedRef, self).__init__(ob, callback) |
| 134 | self.__counter = 0 |
| 135 | for k, v in annotations.iteritems(): |
| 136 | setattr(self, k, v) |
| 137 | |
| 138 | def __call__(self): |
| 139 | """Return a pair containing the referent and the number of |
| 140 | times the reference has been called. |
| 141 | """ |
| 142 | ob = super(ExtendedRef, self).__call__() |
| 143 | if ob is not None: |
| 144 | self.__counter += 1 |
| 145 | ob = (ob, self.__counter) |
| 146 | return ob |
| 147 | </pre></div> |
| 148 | |
| 149 | <P> |
| 150 | |
| 151 | <DIV CLASS="navigation"> |
| 152 | <div class='online-navigation'> |
| 153 | <p></p><hr /> |
| 154 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 155 | <tr> |
| 156 | <td class='online-navigation'><a rel="prev" title="3.3 weakref " |
| 157 | href="module-weakref.html"><img src='../icons/previous.png' |
| 158 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 159 | <td class='online-navigation'><a rel="parent" title="3.3 weakref " |
| 160 | href="module-weakref.html"><img src='../icons/up.png' |
| 161 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 162 | <td class='online-navigation'><a rel="next" title="3.3.2 Example" |
| 163 | href="weakref-example.html"><img src='../icons/next.png' |
| 164 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 165 | <td align="center" width="100%">Python Library Reference</td> |
| 166 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 167 | href="contents.html"><img src='../icons/contents.png' |
| 168 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 169 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 170 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 171 | <td class='online-navigation'><a rel="index" title="Index" |
| 172 | href="genindex.html"><img src='../icons/index.png' |
| 173 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 174 | </tr></table> |
| 175 | <div class='online-navigation'> |
| 176 | <b class="navlabel">Previous:</b> |
| 177 | <a class="sectref" rel="prev" href="module-weakref.html">3.3 weakref </A> |
| 178 | <b class="navlabel">Up:</b> |
| 179 | <a class="sectref" rel="parent" href="module-weakref.html">3.3 weakref </A> |
| 180 | <b class="navlabel">Next:</b> |
| 181 | <a class="sectref" rel="next" href="weakref-example.html">3.3.2 Example</A> |
| 182 | </div> |
| 183 | </div> |
| 184 | <hr /> |
| 185 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 186 | </DIV> |
| 187 | <!--End of Navigation Panel--> |
| 188 | <ADDRESS> |
| 189 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 190 | </ADDRESS> |
| 191 | </BODY> |
| 192 | </HTML> |