gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

904 lines
77KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="weakref — Weak references" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/weakref.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/weakref.py The weakref module allows the Python programmer to create weak references to objects. In the following, the term referent means the object which is referred to by a weak..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/weakref.py The weakref module allows the Python programmer to create weak references to objects. In the following, the term referent means the object which is referred to by a weak..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>weakref — Weak references &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="types — Dynamic type creation and names for built-in types" href="types.html" />
  33. <link rel="prev" title="array — Efficient arrays of numeric values" href="array.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/weakref.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a><ul>
  86. <li><a class="reference internal" href="#weak-reference-objects">Weak Reference Objects</a></li>
  87. <li><a class="reference internal" href="#example">Example</a></li>
  88. <li><a class="reference internal" href="#finalizer-objects">Finalizer Objects</a></li>
  89. <li><a class="reference internal" href="#comparing-finalizers-with-del-methods">Comparing finalizers with <code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code> methods</a></li>
  90. </ul>
  91. </li>
  92. </ul>
  93. </div>
  94. <div>
  95. <h4>Previous topic</h4>
  96. <p class="topless"><a href="array.html"
  97. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code> — Efficient arrays of numeric values</a></p>
  98. </div>
  99. <div>
  100. <h4>Next topic</h4>
  101. <p class="topless"><a href="types.html"
  102. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types</a></p>
  103. </div>
  104. <div role="note" aria-label="source link">
  105. <h3>This Page</h3>
  106. <ul class="this-page-menu">
  107. <li><a href="../bugs.html">Report a Bug</a></li>
  108. <li>
  109. <a href="https://github.com/python/cpython/blob/main/Doc/library/weakref.rst"
  110. rel="nofollow">Show Source
  111. </a>
  112. </li>
  113. </ul>
  114. </div>
  115. </nav>
  116. </div>
  117. </div>
  118. <div class="related" role="navigation" aria-label="related navigation">
  119. <h3>Navigation</h3>
  120. <ul>
  121. <li class="right" style="margin-right: 10px">
  122. <a href="../genindex.html" title="General Index"
  123. accesskey="I">index</a></li>
  124. <li class="right" >
  125. <a href="../py-modindex.html" title="Python Module Index"
  126. >modules</a> |</li>
  127. <li class="right" >
  128. <a href="types.html" title="types — Dynamic type creation and names for built-in types"
  129. accesskey="N">next</a> |</li>
  130. <li class="right" >
  131. <a href="array.html" title="array — Efficient arrays of numeric values"
  132. accesskey="P">previous</a> |</li>
  133. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  134. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  135. <li class="switchers">
  136. <div class="language_switcher_placeholder"></div>
  137. <div class="version_switcher_placeholder"></div>
  138. </li>
  139. <li>
  140. </li>
  141. <li id="cpython-language-and-version">
  142. <a href="../index.html">3.12.3 Documentation</a> &#187;
  143. </li>
  144. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  145. <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> &#187;</li>
  146. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></li>
  147. <li class="right">
  148. <div class="inline-search" role="search">
  149. <form class="inline-search" action="../search.html" method="get">
  150. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  151. <input type="submit" value="Go" />
  152. </form>
  153. </div>
  154. |
  155. </li>
  156. <li class="right">
  157. <label class="theme-selector-label">
  158. Theme
  159. <select class="theme-selector" oninput="activateTheme(this.value)">
  160. <option value="auto" selected>Auto</option>
  161. <option value="light">Light</option>
  162. <option value="dark">Dark</option>
  163. </select>
  164. </label> |</li>
  165. </ul>
  166. </div>
  167. <div class="document">
  168. <div class="documentwrapper">
  169. <div class="bodywrapper">
  170. <div class="body" role="main">
  171. <section id="module-weakref">
  172. <span id="weakref-weak-references"></span><span id="mod-weakref"></span><h1><a class="reference internal" href="#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> — Weak references<a class="headerlink" href="#module-weakref" title="Link to this heading">¶</a></h1>
  173. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/weakref.py">Lib/weakref.py</a></p>
  174. <hr class="docutils" />
  175. <p>The <a class="reference internal" href="#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module allows the Python programmer to create <em class="dfn">weak
  176. references</em> to objects.</p>
  177. <p>In the following, the term <em class="dfn">referent</em> means the object which is referred to
  178. by a weak reference.</p>
  179. <p>A weak reference to an object is not enough to keep the object alive: when the
  180. only remaining references to a referent are weak references,
  181. <a class="reference internal" href="../glossary.html#term-garbage-collection"><span class="xref std std-term">garbage collection</span></a> is free to destroy the referent and reuse its memory
  182. for something else. However, until the object is actually destroyed the weak
  183. reference may return the object even if there are no strong references to it.</p>
  184. <p>A primary use for weak references is to implement caches or
  185. mappings holding large objects, where it’s desired that a large object not be
  186. kept alive solely because it appears in a cache or mapping.</p>
  187. <p>For example, if you have a number of large binary image objects, you may wish to
  188. associate a name with each. If you used a Python dictionary to map names to
  189. images, or images to names, the image objects would remain alive just because
  190. they appeared as values or keys in the dictionaries. The
  191. <a class="reference internal" href="#weakref.WeakKeyDictionary" title="weakref.WeakKeyDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakKeyDictionary</span></code></a> and <a class="reference internal" href="#weakref.WeakValueDictionary" title="weakref.WeakValueDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakValueDictionary</span></code></a> classes supplied by
  192. the <a class="reference internal" href="#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module are an alternative, using weak references to construct
  193. mappings that don’t keep objects alive solely because they appear in the mapping
  194. objects. If, for example, an image object is a value in a
  195. <a class="reference internal" href="#weakref.WeakValueDictionary" title="weakref.WeakValueDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakValueDictionary</span></code></a>, then when the last remaining references to that
  196. image object are the weak references held by weak mappings, garbage collection
  197. can reclaim the object, and its corresponding entries in weak mappings are
  198. simply deleted.</p>
  199. <p><a class="reference internal" href="#weakref.WeakKeyDictionary" title="weakref.WeakKeyDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakKeyDictionary</span></code></a> and <a class="reference internal" href="#weakref.WeakValueDictionary" title="weakref.WeakValueDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakValueDictionary</span></code></a> use weak references
  200. in their implementation, setting up callback functions on the weak references
  201. that notify the weak dictionaries when a key or value has been reclaimed by
  202. garbage collection. <a class="reference internal" href="#weakref.WeakSet" title="weakref.WeakSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakSet</span></code></a> implements the <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> interface,
  203. but keeps weak references to its elements, just like a
  204. <a class="reference internal" href="#weakref.WeakKeyDictionary" title="weakref.WeakKeyDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakKeyDictionary</span></code></a> does.</p>
  205. <p><a class="reference internal" href="#weakref.finalize" title="weakref.finalize"><code class="xref py py-class docutils literal notranslate"><span class="pre">finalize</span></code></a> provides a straight forward way to register a
  206. cleanup function to be called when an object is garbage collected.
  207. This is simpler to use than setting up a callback function on a raw
  208. weak reference, since the module automatically ensures that the finalizer
  209. remains alive until the object is collected.</p>
  210. <p>Most programs should find that using one of these weak container types
  211. or <a class="reference internal" href="#weakref.finalize" title="weakref.finalize"><code class="xref py py-class docutils literal notranslate"><span class="pre">finalize</span></code></a> is all they need – it’s not usually necessary to
  212. create your own weak references directly. The low-level machinery is
  213. exposed by the <a class="reference internal" href="#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a> module for the benefit of advanced uses.</p>
  214. <p>Not all objects can be weakly referenced. Objects which support weak references
  215. include class instances, functions written in Python (but not in C), instance methods,
  216. sets, frozensets, some <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>, <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generators</span></a>,
  217. type objects, sockets, arrays, deques, regular expression pattern objects, and code
  218. objects.</p>
  219. <div class="versionchanged">
  220. <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for thread.lock, threading.Lock, and code objects.</p>
  221. </div>
  222. <p>Several built-in types such as <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> and <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> do not directly
  223. support weak references but can add support through subclassing:</p>
  224. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Dict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
  225. <span class="k">pass</span>
  226. <span class="n">obj</span> <span class="o">=</span> <span class="n">Dict</span><span class="p">(</span><span class="n">red</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">green</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">blue</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># this object is weak referenceable</span>
  227. </pre></div>
  228. </div>
  229. <div class="impl-detail compound">
  230. <p><strong>CPython implementation detail:</strong> Other built-in types such as <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> and <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> do not support weak
  231. references even when subclassed.</p>
  232. </div>
  233. <p>Extension types can easily be made to support weak references; see
  234. <a class="reference internal" href="../extending/newtypes.html#weakref-support"><span class="std std-ref">Weak Reference Support</span></a>.</p>
  235. <p>When <code class="docutils literal notranslate"><span class="pre">__slots__</span></code> are defined for a given type, weak reference support is
  236. disabled unless a <code class="docutils literal notranslate"><span class="pre">'__weakref__'</span></code> string is also present in the sequence of
  237. strings in the <code class="docutils literal notranslate"><span class="pre">__slots__</span></code> declaration.
  238. See <a class="reference internal" href="../reference/datamodel.html#slots"><span class="std std-ref">__slots__ documentation</span></a> for details.</p>
  239. <dl class="py class">
  240. <dt class="sig sig-object py" id="weakref.ref">
  241. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">ref</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.ref" title="Link to this definition">¶</a></dt>
  242. <dd><p>Return a weak reference to <em>object</em>. The original object can be retrieved by
  243. calling the reference object if the referent is still alive; if the referent is
  244. no longer alive, calling the reference object will cause <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> to be
  245. returned. If <em>callback</em> is provided and not <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>, and the returned
  246. weakref object is still alive, the callback will be called when the object is
  247. about to be finalized; the weak reference object will be passed as the only
  248. parameter to the callback; the referent will no longer be available.</p>
  249. <p>It is allowable for many weak references to be constructed for the same object.
  250. Callbacks registered for each weak reference will be called from the most
  251. recently registered callback to the oldest registered callback.</p>
  252. <p>Exceptions raised by the callback will be noted on the standard error output,
  253. but cannot be propagated; they are handled in exactly the same way as exceptions
  254. raised from an object’s <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method.</p>
  255. <p>Weak references are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> if the <em>object</em> is hashable. They will
  256. maintain their hash value even after the <em>object</em> was deleted. If
  257. <a class="reference internal" href="functions.html#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a> is called the first time only after the <em>object</em> was deleted,
  258. the call will raise <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  259. <p>Weak references support tests for equality, but not ordering. If the referents
  260. are still alive, two references have the same equality relationship as their
  261. referents (regardless of the <em>callback</em>). If either referent has been deleted,
  262. the references are equal only if the reference objects are the same object.</p>
  263. <p>This is a subclassable type rather than a factory function.</p>
  264. <dl class="py attribute">
  265. <dt class="sig sig-object py" id="weakref.ref.__callback__">
  266. <span class="sig-name descname"><span class="pre">__callback__</span></span><a class="headerlink" href="#weakref.ref.__callback__" title="Link to this definition">¶</a></dt>
  267. <dd><p>This read-only attribute returns the callback currently associated to the
  268. weakref. If there is no callback or if the referent of the weakref is
  269. no longer alive then this attribute will have value <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  270. </dd></dl>
  271. <div class="versionchanged">
  272. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added the <a class="reference internal" href="#weakref.ref.__callback__" title="weakref.ref.__callback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__callback__</span></code></a> attribute.</p>
  273. </div>
  274. </dd></dl>
  275. <dl class="py function">
  276. <dt class="sig sig-object py" id="weakref.proxy">
  277. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">proxy</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.proxy" title="Link to this definition">¶</a></dt>
  278. <dd><p>Return a proxy to <em>object</em> which uses a weak reference. This supports use of
  279. the proxy in most contexts instead of requiring the explicit dereferencing used
  280. with weak reference objects. The returned object will have a type of either
  281. <code class="docutils literal notranslate"><span class="pre">ProxyType</span></code> or <code class="docutils literal notranslate"><span class="pre">CallableProxyType</span></code>, depending on whether <em>object</em> is
  282. callable. Proxy objects are not <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> regardless of the referent; this
  283. avoids a number of problems related to their fundamentally mutable nature, and
  284. prevents their use as dictionary keys. <em>callback</em> is the same as the parameter
  285. of the same name to the <a class="reference internal" href="#weakref.ref" title="weakref.ref"><code class="xref py py-func docutils literal notranslate"><span class="pre">ref()</span></code></a> function.</p>
  286. <p>Accessing an attribute of the proxy object after the referent is
  287. garbage collected raises <a class="reference internal" href="exceptions.html#ReferenceError" title="ReferenceError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ReferenceError</span></code></a>.</p>
  288. <div class="versionchanged">
  289. <p><span class="versionmodified changed">Changed in version 3.8: </span>Extended the operator support on proxy objects to include the matrix
  290. multiplication operators <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> and <code class="docutils literal notranslate"><span class="pre">&#64;=</span></code>.</p>
  291. </div>
  292. </dd></dl>
  293. <dl class="py function">
  294. <dt class="sig sig-object py" id="weakref.getweakrefcount">
  295. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">getweakrefcount</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#weakref.getweakrefcount" title="Link to this definition">¶</a></dt>
  296. <dd><p>Return the number of weak references and proxies which refer to <em>object</em>.</p>
  297. </dd></dl>
  298. <dl class="py function">
  299. <dt class="sig sig-object py" id="weakref.getweakrefs">
  300. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">getweakrefs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#weakref.getweakrefs" title="Link to this definition">¶</a></dt>
  301. <dd><p>Return a list of all weak reference and proxy objects which refer to <em>object</em>.</p>
  302. </dd></dl>
  303. <dl class="py class">
  304. <dt class="sig sig-object py" id="weakref.WeakKeyDictionary">
  305. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">WeakKeyDictionary</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">dict</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakKeyDictionary" title="Link to this definition">¶</a></dt>
  306. <dd><p>Mapping class that references keys weakly. Entries in the dictionary will be
  307. discarded when there is no longer a strong reference to the key. This can be
  308. used to associate additional data with an object owned by other parts of an
  309. application without adding attributes to those objects. This can be especially
  310. useful with objects that override attribute accesses.</p>
  311. <p>Note that when a key with equal value to an existing key (but not equal identity)
  312. is inserted into the dictionary, it replaces the value but does not replace the
  313. existing key. Due to this, when the reference to the original key is deleted, it
  314. also deletes the entry in the dictionary:</p>
  315. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">T</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span> <span class="k">pass</span>
  316. <span class="gp">...</span>
  317. <span class="gp">&gt;&gt;&gt; </span><span class="n">k1</span><span class="p">,</span> <span class="n">k2</span> <span class="o">=</span> <span class="n">T</span><span class="p">(),</span> <span class="n">T</span><span class="p">()</span>
  318. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakKeyDictionary</span><span class="p">()</span>
  319. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">k1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># d = {k1: 1}</span>
  320. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">k2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># d = {k1: 2}</span>
  321. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">k1</span> <span class="c1"># d = {}</span>
  322. </pre></div>
  323. </div>
  324. <p>A workaround would be to remove the key prior to reassignment:</p>
  325. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">T</span><span class="p">(</span><span class="nb">str</span><span class="p">):</span> <span class="k">pass</span>
  326. <span class="gp">...</span>
  327. <span class="gp">&gt;&gt;&gt; </span><span class="n">k1</span><span class="p">,</span> <span class="n">k2</span> <span class="o">=</span> <span class="n">T</span><span class="p">(),</span> <span class="n">T</span><span class="p">()</span>
  328. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakKeyDictionary</span><span class="p">()</span>
  329. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">k1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span> <span class="c1"># d = {k1: 1}</span>
  330. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="n">k1</span><span class="p">]</span>
  331. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="n">k2</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span> <span class="c1"># d = {k2: 2}</span>
  332. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">k1</span> <span class="c1"># d = {k2: 2}</span>
  333. </pre></div>
  334. </div>
  335. <div class="versionchanged">
  336. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for <code class="docutils literal notranslate"><span class="pre">|</span></code> and <code class="docutils literal notranslate"><span class="pre">|=</span></code> operators, specified in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>.</p>
  337. </div>
  338. </dd></dl>
  339. <p><a class="reference internal" href="#weakref.WeakKeyDictionary" title="weakref.WeakKeyDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakKeyDictionary</span></code></a> objects have an additional method that
  340. exposes the internal references directly. The references are not guaranteed to
  341. be “live” at the time they are used, so the result of calling the references
  342. needs to be checked before being used. This can be used to avoid creating
  343. references that will cause the garbage collector to keep the keys around longer
  344. than needed.</p>
  345. <dl class="py method">
  346. <dt class="sig sig-object py" id="weakref.WeakKeyDictionary.keyrefs">
  347. <span class="sig-prename descclassname"><span class="pre">WeakKeyDictionary.</span></span><span class="sig-name descname"><span class="pre">keyrefs</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakKeyDictionary.keyrefs" title="Link to this definition">¶</a></dt>
  348. <dd><p>Return an iterable of the weak references to the keys.</p>
  349. </dd></dl>
  350. <dl class="py class">
  351. <dt class="sig sig-object py" id="weakref.WeakValueDictionary">
  352. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">WeakValueDictionary</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">dict</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakValueDictionary" title="Link to this definition">¶</a></dt>
  353. <dd><p>Mapping class that references values weakly. Entries in the dictionary will be
  354. discarded when no strong reference to the value exists any more.</p>
  355. <div class="versionchanged">
  356. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for <code class="docutils literal notranslate"><span class="pre">|</span></code> and <code class="docutils literal notranslate"><span class="pre">|=</span></code> operators, as specified in <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>.</p>
  357. </div>
  358. </dd></dl>
  359. <p><a class="reference internal" href="#weakref.WeakValueDictionary" title="weakref.WeakValueDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakValueDictionary</span></code></a> objects have an additional method that has the
  360. same issues as the <a class="reference internal" href="#weakref.WeakKeyDictionary.keyrefs" title="weakref.WeakKeyDictionary.keyrefs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">WeakKeyDictionary.keyrefs()</span></code></a> method.</p>
  361. <dl class="py method">
  362. <dt class="sig sig-object py" id="weakref.WeakValueDictionary.valuerefs">
  363. <span class="sig-prename descclassname"><span class="pre">WeakValueDictionary.</span></span><span class="sig-name descname"><span class="pre">valuerefs</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakValueDictionary.valuerefs" title="Link to this definition">¶</a></dt>
  364. <dd><p>Return an iterable of the weak references to the values.</p>
  365. </dd></dl>
  366. <dl class="py class">
  367. <dt class="sig sig-object py" id="weakref.WeakSet">
  368. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">WeakSet</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">elements</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakSet" title="Link to this definition">¶</a></dt>
  369. <dd><p>Set class that keeps weak references to its elements. An element will be
  370. discarded when no strong reference to it exists any more.</p>
  371. </dd></dl>
  372. <dl class="py class">
  373. <dt class="sig sig-object py" id="weakref.WeakMethod">
  374. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">WeakMethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">method</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">callback</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.WeakMethod" title="Link to this definition">¶</a></dt>
  375. <dd><p>A custom <a class="reference internal" href="#weakref.ref" title="weakref.ref"><code class="xref py py-class docutils literal notranslate"><span class="pre">ref</span></code></a> subclass which simulates a weak reference to a bound
  376. method (i.e., a method defined on a class and looked up on an instance).
  377. Since a bound method is ephemeral, a standard weak reference cannot keep
  378. hold of it. <a class="reference internal" href="#weakref.WeakMethod" title="weakref.WeakMethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakMethod</span></code></a> has special code to recreate the bound
  379. method until either the object or the original function dies:</p>
  380. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
  381. <span class="gp">... </span> <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  382. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;method called!&quot;</span><span class="p">)</span>
  383. <span class="gp">...</span>
  384. <span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">C</span><span class="p">()</span>
  385. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
  386. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">()</span>
  387. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakMethod</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
  388. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">()</span>
  389. <span class="go">&lt;bound method C.method of &lt;__main__.C object at 0x7fc859830220&gt;&gt;</span>
  390. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">()()</span>
  391. <span class="go">method called!</span>
  392. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">c</span>
  393. <span class="gp">&gt;&gt;&gt; </span><span class="n">gc</span><span class="o">.</span><span class="n">collect</span><span class="p">()</span>
  394. <span class="go">0</span>
  395. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">()</span>
  396. <span class="gp">&gt;&gt;&gt;</span>
  397. </pre></div>
  398. </div>
  399. <p><em>callback</em> is the same as the parameter of the same name to the <a class="reference internal" href="#weakref.ref" title="weakref.ref"><code class="xref py py-func docutils literal notranslate"><span class="pre">ref()</span></code></a> function.</p>
  400. <div class="versionadded">
  401. <p><span class="versionmodified added">New in version 3.4.</span></p>
  402. </div>
  403. </dd></dl>
  404. <dl class="py class">
  405. <dt class="sig sig-object py" id="weakref.finalize">
  406. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">finalize</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#weakref.finalize" title="Link to this definition">¶</a></dt>
  407. <dd><p>Return a callable finalizer object which will be called when <em>obj</em>
  408. is garbage collected. Unlike an ordinary weak reference, a finalizer
  409. will always survive until the reference object is collected, greatly
  410. simplifying lifecycle management.</p>
  411. <p>A finalizer is considered <em>alive</em> until it is called (either explicitly
  412. or at garbage collection), and after that it is <em>dead</em>. Calling a live
  413. finalizer returns the result of evaluating <code class="docutils literal notranslate"><span class="pre">func(*arg,</span> <span class="pre">**kwargs)</span></code>,
  414. whereas calling a dead finalizer returns <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  415. <p>Exceptions raised by finalizer callbacks during garbage collection
  416. will be shown on the standard error output, but cannot be
  417. propagated. They are handled in the same way as exceptions raised
  418. from an object’s <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method or a weak reference’s
  419. callback.</p>
  420. <p>When the program exits, each remaining live finalizer is called
  421. unless its <a class="reference internal" href="atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-attr docutils literal notranslate"><span class="pre">atexit</span></code></a> attribute has been set to false. They
  422. are called in reverse order of creation.</p>
  423. <p>A finalizer will never invoke its callback during the later part of
  424. the <a class="reference internal" href="../glossary.html#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a> when module globals are liable to have
  425. been replaced by <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  426. <dl class="py method">
  427. <dt class="sig sig-object py" id="weakref.finalize.__call__">
  428. <span class="sig-name descname"><span class="pre">__call__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.finalize.__call__" title="Link to this definition">¶</a></dt>
  429. <dd><p>If <em>self</em> is alive then mark it as dead and return the result of
  430. calling <code class="docutils literal notranslate"><span class="pre">func(*args,</span> <span class="pre">**kwargs)</span></code>. If <em>self</em> is dead then return
  431. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  432. </dd></dl>
  433. <dl class="py method">
  434. <dt class="sig sig-object py" id="weakref.finalize.detach">
  435. <span class="sig-name descname"><span class="pre">detach</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.finalize.detach" title="Link to this definition">¶</a></dt>
  436. <dd><p>If <em>self</em> is alive then mark it as dead and return the tuple
  437. <code class="docutils literal notranslate"><span class="pre">(obj,</span> <span class="pre">func,</span> <span class="pre">args,</span> <span class="pre">kwargs)</span></code>. If <em>self</em> is dead then return
  438. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  439. </dd></dl>
  440. <dl class="py method">
  441. <dt class="sig sig-object py" id="weakref.finalize.peek">
  442. <span class="sig-name descname"><span class="pre">peek</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#weakref.finalize.peek" title="Link to this definition">¶</a></dt>
  443. <dd><p>If <em>self</em> is alive then return the tuple <code class="docutils literal notranslate"><span class="pre">(obj,</span> <span class="pre">func,</span> <span class="pre">args,</span>
  444. <span class="pre">kwargs)</span></code>. If <em>self</em> is dead then return <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.</p>
  445. </dd></dl>
  446. <dl class="py attribute">
  447. <dt class="sig sig-object py" id="weakref.finalize.alive">
  448. <span class="sig-name descname"><span class="pre">alive</span></span><a class="headerlink" href="#weakref.finalize.alive" title="Link to this definition">¶</a></dt>
  449. <dd><p>Property which is true if the finalizer is alive, false otherwise.</p>
  450. </dd></dl>
  451. <dl class="py attribute">
  452. <dt class="sig sig-object py" id="weakref.finalize.atexit">
  453. <span class="sig-name descname"><span class="pre">atexit</span></span><a class="headerlink" href="#weakref.finalize.atexit" title="Link to this definition">¶</a></dt>
  454. <dd><p>A writable boolean property which by default is true. When the
  455. program exits, it calls all remaining live finalizers for which
  456. <a class="reference internal" href="#weakref.finalize.atexit" title="weakref.finalize.atexit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">atexit</span></code></a> is true. They are called in reverse order of
  457. creation.</p>
  458. </dd></dl>
  459. <div class="admonition note">
  460. <p class="admonition-title">Note</p>
  461. <p>It is important to ensure that <em>func</em>, <em>args</em> and <em>kwargs</em> do
  462. not own any references to <em>obj</em>, either directly or indirectly,
  463. since otherwise <em>obj</em> will never be garbage collected. In
  464. particular, <em>func</em> should not be a bound method of <em>obj</em>.</p>
  465. </div>
  466. <div class="versionadded">
  467. <p><span class="versionmodified added">New in version 3.4.</span></p>
  468. </div>
  469. </dd></dl>
  470. <dl class="py data">
  471. <dt class="sig sig-object py" id="weakref.ReferenceType">
  472. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">ReferenceType</span></span><a class="headerlink" href="#weakref.ReferenceType" title="Link to this definition">¶</a></dt>
  473. <dd><p>The type object for weak references objects.</p>
  474. </dd></dl>
  475. <dl class="py data">
  476. <dt class="sig sig-object py" id="weakref.ProxyType">
  477. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">ProxyType</span></span><a class="headerlink" href="#weakref.ProxyType" title="Link to this definition">¶</a></dt>
  478. <dd><p>The type object for proxies of objects which are not callable.</p>
  479. </dd></dl>
  480. <dl class="py data">
  481. <dt class="sig sig-object py" id="weakref.CallableProxyType">
  482. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">CallableProxyType</span></span><a class="headerlink" href="#weakref.CallableProxyType" title="Link to this definition">¶</a></dt>
  483. <dd><p>The type object for proxies of callable objects.</p>
  484. </dd></dl>
  485. <dl class="py data">
  486. <dt class="sig sig-object py" id="weakref.ProxyTypes">
  487. <span class="sig-prename descclassname"><span class="pre">weakref.</span></span><span class="sig-name descname"><span class="pre">ProxyTypes</span></span><a class="headerlink" href="#weakref.ProxyTypes" title="Link to this definition">¶</a></dt>
  488. <dd><p>Sequence containing all the type objects for proxies. This can make it simpler
  489. to test if an object is a proxy without being dependent on naming both proxy
  490. types.</p>
  491. </dd></dl>
  492. <div class="admonition seealso">
  493. <p class="admonition-title">See also</p>
  494. <dl class="simple">
  495. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0205/"><strong>PEP 205</strong></a> - Weak References</dt><dd><p>The proposal and rationale for this feature, including links to earlier
  496. implementations and information about similar features in other languages.</p>
  497. </dd>
  498. </dl>
  499. </div>
  500. <section id="weak-reference-objects">
  501. <span id="weakref-objects"></span><h2>Weak Reference Objects<a class="headerlink" href="#weak-reference-objects" title="Link to this heading">¶</a></h2>
  502. <p>Weak reference objects have no methods and no attributes besides
  503. <a class="reference internal" href="#weakref.ref.__callback__" title="weakref.ref.__callback__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ref.__callback__</span></code></a>. A weak reference object allows the referent to be
  504. obtained, if it still exists, by calling it:</p>
  505. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">weakref</span>
  506. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Object</span><span class="p">:</span>
  507. <span class="gp">... </span> <span class="k">pass</span>
  508. <span class="gp">...</span>
  509. <span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  510. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
  511. <span class="gp">&gt;&gt;&gt; </span><span class="n">o2</span> <span class="o">=</span> <span class="n">r</span><span class="p">()</span>
  512. <span class="gp">&gt;&gt;&gt; </span><span class="n">o</span> <span class="ow">is</span> <span class="n">o2</span>
  513. <span class="go">True</span>
  514. </pre></div>
  515. </div>
  516. <p>If the referent no longer exists, calling the reference object returns
  517. <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>:</p>
  518. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">o</span><span class="p">,</span> <span class="n">o2</span>
  519. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="p">())</span>
  520. <span class="go">None</span>
  521. </pre></div>
  522. </div>
  523. <p>Testing that a weak reference object is still live should be done using the
  524. expression <code class="docutils literal notranslate"><span class="pre">ref()</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></code>. Normally, application code that needs to use
  525. a reference object should follow this pattern:</p>
  526. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># r is a weak reference object</span>
  527. <span class="n">o</span> <span class="o">=</span> <span class="n">r</span><span class="p">()</span>
  528. <span class="k">if</span> <span class="n">o</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
  529. <span class="c1"># referent has been garbage collected</span>
  530. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Object has been deallocated; can&#39;t frobnicate.&quot;</span><span class="p">)</span>
  531. <span class="k">else</span><span class="p">:</span>
  532. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Object is still live!&quot;</span><span class="p">)</span>
  533. <span class="n">o</span><span class="o">.</span><span class="n">do_something_useful</span><span class="p">()</span>
  534. </pre></div>
  535. </div>
  536. <p>Using a separate test for “liveness” creates race conditions in threaded
  537. applications; another thread can cause a weak reference to become invalidated
  538. before the weak reference is called; the idiom shown above is safe in threaded
  539. applications as well as single-threaded applications.</p>
  540. <p>Specialized versions of <a class="reference internal" href="#weakref.ref" title="weakref.ref"><code class="xref py py-class docutils literal notranslate"><span class="pre">ref</span></code></a> objects can be created through subclassing.
  541. This is used in the implementation of the <a class="reference internal" href="#weakref.WeakValueDictionary" title="weakref.WeakValueDictionary"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakValueDictionary</span></code></a> to reduce
  542. the memory overhead for each entry in the mapping. This may be most useful to
  543. associate additional information with a reference, but could also be used to
  544. insert additional processing on calls to retrieve the referent.</p>
  545. <p>This example shows how a subclass of <a class="reference internal" href="#weakref.ref" title="weakref.ref"><code class="xref py py-class docutils literal notranslate"><span class="pre">ref</span></code></a> can be used to store
  546. additional information about an object and affect the value that’s returned when
  547. the referent is accessed:</p>
  548. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">weakref</span>
  549. <span class="k">class</span> <span class="nc">ExtendedRef</span><span class="p">(</span><span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">):</span>
  550. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ob</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">**</span><span class="n">annotations</span><span class="p">):</span>
  551. <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">ob</span><span class="p">,</span> <span class="n">callback</span><span class="p">)</span>
  552. <span class="bp">self</span><span class="o">.</span><span class="n">__counter</span> <span class="o">=</span> <span class="mi">0</span>
  553. <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">annotations</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
  554. <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
  555. <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  556. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Return a pair containing the referent and the number of</span>
  557. <span class="sd"> times the reference has been called.</span>
  558. <span class="sd"> &quot;&quot;&quot;</span>
  559. <span class="n">ob</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__call__</span><span class="p">()</span>
  560. <span class="k">if</span> <span class="n">ob</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  561. <span class="bp">self</span><span class="o">.</span><span class="n">__counter</span> <span class="o">+=</span> <span class="mi">1</span>
  562. <span class="n">ob</span> <span class="o">=</span> <span class="p">(</span><span class="n">ob</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__counter</span><span class="p">)</span>
  563. <span class="k">return</span> <span class="n">ob</span>
  564. </pre></div>
  565. </div>
  566. </section>
  567. <section id="example">
  568. <span id="weakref-example"></span><h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
  569. <p>This simple example shows how an application can use object IDs to retrieve
  570. objects that it has seen before. The IDs of the objects can then be used in
  571. other data structures without forcing the objects to remain alive, but the
  572. objects can still be retrieved by ID if they do.</p>
  573. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">weakref</span>
  574. <span class="n">_id2obj_dict</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">WeakValueDictionary</span><span class="p">()</span>
  575. <span class="k">def</span> <span class="nf">remember</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
  576. <span class="n">oid</span> <span class="o">=</span> <span class="nb">id</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
  577. <span class="n">_id2obj_dict</span><span class="p">[</span><span class="n">oid</span><span class="p">]</span> <span class="o">=</span> <span class="n">obj</span>
  578. <span class="k">return</span> <span class="n">oid</span>
  579. <span class="k">def</span> <span class="nf">id2obj</span><span class="p">(</span><span class="n">oid</span><span class="p">):</span>
  580. <span class="k">return</span> <span class="n">_id2obj_dict</span><span class="p">[</span><span class="n">oid</span><span class="p">]</span>
  581. </pre></div>
  582. </div>
  583. </section>
  584. <section id="finalizer-objects">
  585. <span id="finalize-examples"></span><h2>Finalizer Objects<a class="headerlink" href="#finalizer-objects" title="Link to this heading">¶</a></h2>
  586. <p>The main benefit of using <a class="reference internal" href="#weakref.finalize" title="weakref.finalize"><code class="xref py py-class docutils literal notranslate"><span class="pre">finalize</span></code></a> is that it makes it simple
  587. to register a callback without needing to preserve the returned finalizer
  588. object. For instance</p>
  589. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">weakref</span>
  590. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Object</span><span class="p">:</span>
  591. <span class="gp">... </span> <span class="k">pass</span>
  592. <span class="gp">...</span>
  593. <span class="gp">&gt;&gt;&gt; </span><span class="n">kenny</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  594. <span class="gp">&gt;&gt;&gt; </span><span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">kenny</span><span class="p">,</span> <span class="nb">print</span><span class="p">,</span> <span class="s2">&quot;You killed Kenny!&quot;</span><span class="p">)</span>
  595. <span class="go">&lt;finalize object at ...; for &#39;Object&#39; at ...&gt;</span>
  596. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">kenny</span>
  597. <span class="go">You killed Kenny!</span>
  598. </pre></div>
  599. </div>
  600. <p>The finalizer can be called directly as well. However the finalizer
  601. will invoke the callback at most once.</p>
  602. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">callback</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">):</span>
  603. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;CALLBACK&quot;</span><span class="p">)</span>
  604. <span class="gp">... </span> <span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span>
  605. <span class="gp">...</span>
  606. <span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  607. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  608. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">f</span><span class="o">.</span><span class="n">alive</span>
  609. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">f</span><span class="p">()</span> <span class="o">==</span> <span class="mi">6</span>
  610. <span class="go">CALLBACK</span>
  611. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">alive</span>
  612. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">()</span> <span class="c1"># callback not called because finalizer dead</span>
  613. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">obj</span> <span class="c1"># callback not called because finalizer dead</span>
  614. </pre></div>
  615. </div>
  616. <p>You can unregister a finalizer using its <a class="reference internal" href="#weakref.finalize.detach" title="weakref.finalize.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a>
  617. method. This kills the finalizer and returns the arguments passed to
  618. the constructor when it was created.</p>
  619. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  620. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">callback</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  621. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">detach</span><span class="p">()</span>
  622. <span class="go">(&lt;...Object object ...&gt;, &lt;function callback ...&gt;, (1, 2), {&#39;z&#39;: 3})</span>
  623. <span class="gp">&gt;&gt;&gt; </span><span class="n">newobj</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="o">=</span> <span class="n">_</span>
  624. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">alive</span>
  625. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">newobj</span> <span class="ow">is</span> <span class="n">obj</span>
  626. <span class="gp">&gt;&gt;&gt; </span><span class="k">assert</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">==</span> <span class="mi">6</span>
  627. <span class="go">CALLBACK</span>
  628. </pre></div>
  629. </div>
  630. <p>Unless you set the <a class="reference internal" href="#weakref.finalize.atexit" title="weakref.finalize.atexit"><code class="xref py py-attr docutils literal notranslate"><span class="pre">atexit</span></code></a> attribute to
  631. <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a>, a finalizer will be called when the program exits if it
  632. is still alive. For instance</p>
  633. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="n">Object</span><span class="p">()</span>
  634. <span class="gp">&gt;&gt;&gt; </span><span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">print</span><span class="p">,</span> <span class="s2">&quot;obj dead or exiting&quot;</span><span class="p">)</span>
  635. <span class="go">&lt;finalize object at ...; for &#39;Object&#39; at ...&gt;</span>
  636. <span class="gp">&gt;&gt;&gt; </span><span class="n">exit</span><span class="p">()</span>
  637. <span class="go">obj dead or exiting</span>
  638. </pre></div>
  639. </div>
  640. </section>
  641. <section id="comparing-finalizers-with-del-methods">
  642. <h2>Comparing finalizers with <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> methods<a class="headerlink" href="#comparing-finalizers-with-del-methods" title="Link to this heading">¶</a></h2>
  643. <p>Suppose we want to create a class whose instances represent temporary
  644. directories. The directories should be deleted with their contents
  645. when the first of the following events occurs:</p>
  646. <ul class="simple">
  647. <li><p>the object is garbage collected,</p></li>
  648. <li><p>the object’s <code class="xref py py-meth docutils literal notranslate"><span class="pre">remove()</span></code> method is called, or</p></li>
  649. <li><p>the program exits.</p></li>
  650. </ul>
  651. <p>We might try to implement the class using a <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method as
  652. follows:</p>
  653. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TempDir</span><span class="p">:</span>
  654. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  655. <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
  656. <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  657. <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
  658. <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  659. <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="kc">None</span>
  660. <span class="nd">@property</span>
  661. <span class="k">def</span> <span class="nf">removed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  662. <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="kc">None</span>
  663. <span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  664. <span class="bp">self</span><span class="o">.</span><span class="n">remove</span><span class="p">()</span>
  665. </pre></div>
  666. </div>
  667. <p>Starting with Python 3.4, <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> methods no longer prevent
  668. reference cycles from being garbage collected, and module globals are
  669. no longer forced to <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> during <a class="reference internal" href="../glossary.html#term-interpreter-shutdown"><span class="xref std std-term">interpreter shutdown</span></a>.
  670. So this code should work without any issues on CPython.</p>
  671. <p>However, handling of <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> methods is notoriously implementation
  672. specific, since it depends on internal details of the interpreter’s garbage
  673. collector implementation.</p>
  674. <p>A more robust alternative can be to define a finalizer which only references
  675. the specific functions and objects that it needs, rather than having access
  676. to the full state of the object:</p>
  677. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TempDir</span><span class="p">:</span>
  678. <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  679. <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
  680. <span class="bp">self</span><span class="o">.</span><span class="n">_finalizer</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  681. <span class="k">def</span> <span class="nf">remove</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  682. <span class="bp">self</span><span class="o">.</span><span class="n">_finalizer</span><span class="p">()</span>
  683. <span class="nd">@property</span>
  684. <span class="k">def</span> <span class="nf">removed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  685. <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_finalizer</span><span class="o">.</span><span class="n">alive</span>
  686. </pre></div>
  687. </div>
  688. <p>Defined like this, our finalizer only receives a reference to the details
  689. it needs to clean up the directory appropriately. If the object never gets
  690. garbage collected the finalizer will still be called at exit.</p>
  691. <p>The other advantage of weakref based finalizers is that they can be used to
  692. register finalizers for classes where the definition is controlled by a
  693. third party, such as running code when a module is unloaded:</p>
  694. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">weakref</span><span class="o">,</span> <span class="nn">sys</span>
  695. <span class="k">def</span> <span class="nf">unloading_module</span><span class="p">():</span>
  696. <span class="c1"># implicit reference to the module globals from the function body</span>
  697. <span class="n">weakref</span><span class="o">.</span><span class="n">finalize</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="vm">__name__</span><span class="p">],</span> <span class="n">unloading_module</span><span class="p">)</span>
  698. </pre></div>
  699. </div>
  700. <div class="admonition note">
  701. <p class="admonition-title">Note</p>
  702. <p>If you create a finalizer object in a daemonic thread just as the program
  703. exits then there is the possibility that the finalizer
  704. does not get called at exit. However, in a daemonic thread
  705. <a class="reference internal" href="atexit.html#atexit.register" title="atexit.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">atexit.register()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">try:</span> <span class="pre">...</span> <span class="pre">finally:</span> <span class="pre">...</span></code> and <code class="docutils literal notranslate"><span class="pre">with:</span> <span class="pre">...</span></code>
  706. do not guarantee that cleanup occurs either.</p>
  707. </div>
  708. </section>
  709. </section>
  710. <div class="clearer"></div>
  711. </div>
  712. </div>
  713. </div>
  714. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  715. <div class="sphinxsidebarwrapper">
  716. <div>
  717. <h3><a href="../contents.html">Table of Contents</a></h3>
  718. <ul>
  719. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a><ul>
  720. <li><a class="reference internal" href="#weak-reference-objects">Weak Reference Objects</a></li>
  721. <li><a class="reference internal" href="#example">Example</a></li>
  722. <li><a class="reference internal" href="#finalizer-objects">Finalizer Objects</a></li>
  723. <li><a class="reference internal" href="#comparing-finalizers-with-del-methods">Comparing finalizers with <code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code> methods</a></li>
  724. </ul>
  725. </li>
  726. </ul>
  727. </div>
  728. <div>
  729. <h4>Previous topic</h4>
  730. <p class="topless"><a href="array.html"
  731. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code> — Efficient arrays of numeric values</a></p>
  732. </div>
  733. <div>
  734. <h4>Next topic</h4>
  735. <p class="topless"><a href="types.html"
  736. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">types</span></code> — Dynamic type creation and names for built-in types</a></p>
  737. </div>
  738. <div role="note" aria-label="source link">
  739. <h3>This Page</h3>
  740. <ul class="this-page-menu">
  741. <li><a href="../bugs.html">Report a Bug</a></li>
  742. <li>
  743. <a href="https://github.com/python/cpython/blob/main/Doc/library/weakref.rst"
  744. rel="nofollow">Show Source
  745. </a>
  746. </li>
  747. </ul>
  748. </div>
  749. </div>
  750. <div id="sidebarbutton" title="Collapse sidebar">
  751. <span>«</span>
  752. </div>
  753. </div>
  754. <div class="clearer"></div>
  755. </div>
  756. <div class="related" role="navigation" aria-label="related navigation">
  757. <h3>Navigation</h3>
  758. <ul>
  759. <li class="right" style="margin-right: 10px">
  760. <a href="../genindex.html" title="General Index"
  761. >index</a></li>
  762. <li class="right" >
  763. <a href="../py-modindex.html" title="Python Module Index"
  764. >modules</a> |</li>
  765. <li class="right" >
  766. <a href="types.html" title="types — Dynamic type creation and names for built-in types"
  767. >next</a> |</li>
  768. <li class="right" >
  769. <a href="array.html" title="array — Efficient arrays of numeric values"
  770. >previous</a> |</li>
  771. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  772. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  773. <li class="switchers">
  774. <div class="language_switcher_placeholder"></div>
  775. <div class="version_switcher_placeholder"></div>
  776. </li>
  777. <li>
  778. </li>
  779. <li id="cpython-language-and-version">
  780. <a href="../index.html">3.12.3 Documentation</a> &#187;
  781. </li>
  782. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  783. <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> &#187;</li>
  784. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></li>
  785. <li class="right">
  786. <div class="inline-search" role="search">
  787. <form class="inline-search" action="../search.html" method="get">
  788. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  789. <input type="submit" value="Go" />
  790. </form>
  791. </div>
  792. |
  793. </li>
  794. <li class="right">
  795. <label class="theme-selector-label">
  796. Theme
  797. <select class="theme-selector" oninput="activateTheme(this.value)">
  798. <option value="auto" selected>Auto</option>
  799. <option value="light">Light</option>
  800. <option value="dark">Dark</option>
  801. </select>
  802. </label> |</li>
  803. </ul>
  804. </div>
  805. <div class="footer">
  806. &copy;
  807. <a href="../copyright.html">
  808. Copyright
  809. </a>
  810. 2001-2024, Python Software Foundation.
  811. <br />
  812. This page is licensed under the Python Software Foundation License Version 2.
  813. <br />
  814. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  815. <br />
  816. See <a href="/license.html">History and License</a> for more information.<br />
  817. <br />
  818. The Python Software Foundation is a non-profit corporation.
  819. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  820. <br />
  821. <br />
  822. Last updated on Apr 09, 2024 (13:47 UTC).
  823. <a href="/bugs.html">Found a bug</a>?
  824. <br />
  825. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  826. </div>
  827. </body>
  828. </html>
上海开阖软件有限公司 沪ICP备12045867号-1