gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

902 lines
89KB

  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="collections.abc — Abstract Base Classes for Containers" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/collections.abc.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/_collections_abc.py This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whet..." />
  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/_collections_abc.py This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whet..." />
  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>collections.abc — Abstract Base Classes for Containers &#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="heapq — Heap queue algorithm" href="heapq.html" />
  33. <link rel="prev" title="collections — Container datatypes" href="collections.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/collections.abc.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">collections.abc</span></code> — Abstract Base Classes for Containers</a><ul>
  86. <li><a class="reference internal" href="#collections-abstract-base-classes">Collections Abstract Base Classes</a></li>
  87. <li><a class="reference internal" href="#collections-abstract-base-classes-detailed-descriptions">Collections Abstract Base Classes – Detailed Descriptions</a></li>
  88. <li><a class="reference internal" href="#examples-and-recipes">Examples and Recipes</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </div>
  93. <div>
  94. <h4>Previous topic</h4>
  95. <p class="topless"><a href="collections.html"
  96. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code> — Container datatypes</a></p>
  97. </div>
  98. <div>
  99. <h4>Next topic</h4>
  100. <p class="topless"><a href="heapq.html"
  101. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code> — Heap queue algorithm</a></p>
  102. </div>
  103. <div role="note" aria-label="source link">
  104. <h3>This Page</h3>
  105. <ul class="this-page-menu">
  106. <li><a href="../bugs.html">Report a Bug</a></li>
  107. <li>
  108. <a href="https://github.com/python/cpython/blob/main/Doc/library/collections.abc.rst"
  109. rel="nofollow">Show Source
  110. </a>
  111. </li>
  112. </ul>
  113. </div>
  114. </nav>
  115. </div>
  116. </div>
  117. <div class="related" role="navigation" aria-label="related navigation">
  118. <h3>Navigation</h3>
  119. <ul>
  120. <li class="right" style="margin-right: 10px">
  121. <a href="../genindex.html" title="General Index"
  122. accesskey="I">index</a></li>
  123. <li class="right" >
  124. <a href="../py-modindex.html" title="Python Module Index"
  125. >modules</a> |</li>
  126. <li class="right" >
  127. <a href="heapq.html" title="heapq — Heap queue algorithm"
  128. accesskey="N">next</a> |</li>
  129. <li class="right" >
  130. <a href="collections.html" title="collections — Container datatypes"
  131. accesskey="P">previous</a> |</li>
  132. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  133. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  134. <li class="switchers">
  135. <div class="language_switcher_placeholder"></div>
  136. <div class="version_switcher_placeholder"></div>
  137. </li>
  138. <li>
  139. </li>
  140. <li id="cpython-language-and-version">
  141. <a href="../index.html">3.12.3 Documentation</a> &#187;
  142. </li>
  143. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  144. <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> &#187;</li>
  145. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code> — Abstract Base Classes for Containers</a></li>
  146. <li class="right">
  147. <div class="inline-search" role="search">
  148. <form class="inline-search" action="../search.html" method="get">
  149. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  150. <input type="submit" value="Go" />
  151. </form>
  152. </div>
  153. |
  154. </li>
  155. <li class="right">
  156. <label class="theme-selector-label">
  157. Theme
  158. <select class="theme-selector" oninput="activateTheme(this.value)">
  159. <option value="auto" selected>Auto</option>
  160. <option value="light">Light</option>
  161. <option value="dark">Dark</option>
  162. </select>
  163. </label> |</li>
  164. </ul>
  165. </div>
  166. <div class="document">
  167. <div class="documentwrapper">
  168. <div class="bodywrapper">
  169. <div class="body" role="main">
  170. <section id="module-collections.abc">
  171. <span id="collections-abc-abstract-base-classes-for-containers"></span><h1><a class="reference internal" href="#module-collections.abc" title="collections.abc: Abstract base classes for containers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code></a> — Abstract Base Classes for Containers<a class="headerlink" href="#module-collections.abc" title="Link to this heading">¶</a></h1>
  172. <div class="versionadded">
  173. <p><span class="versionmodified added">New in version 3.3: </span>Formerly, this module was part of the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.</p>
  174. </div>
  175. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/_collections_abc.py">Lib/_collections_abc.py</a></p>
  176. <hr class="docutils" />
  177. <p>This module provides <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base classes</span></a> that
  178. can be used to test whether a class provides a particular interface; for
  179. example, whether it is <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> or whether it is a <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a>.</p>
  180. <p>An <a class="reference internal" href="functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a> or <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> test for an interface works in one
  181. of three ways.</p>
  182. <p>1) A newly written class can inherit directly from one of the
  183. abstract base classes. The class must supply the required abstract
  184. methods. The remaining mixin methods come from inheritance and can be
  185. overridden if desired. Other methods may be added as needed:</p>
  186. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">Sequence</span><span class="p">):</span> <span class="c1"># Direct inheritance</span>
  187. <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="o">...</span> <span class="c1"># Extra method not required by the ABC</span>
  188. <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Required abstract method</span>
  189. <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Required abstract method</span>
  190. <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Optionally override a mixin method</span>
  191. </pre></div>
  192. </div>
  193. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">issubclass</span><span class="p">(</span><span class="n">C</span><span class="p">,</span> <span class="n">Sequence</span><span class="p">)</span>
  194. <span class="go">True</span>
  195. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">C</span><span class="p">(),</span> <span class="n">Sequence</span><span class="p">)</span>
  196. <span class="go">True</span>
  197. </pre></div>
  198. </div>
  199. <p>2) Existing classes and built-in classes can be registered as “virtual
  200. subclasses” of the ABCs. Those classes should define the full API
  201. including all of the abstract methods and all of the mixin methods.
  202. This lets users rely on <a class="reference internal" href="functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a> or <a class="reference internal" href="functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a> tests
  203. to determine whether the full interface is supported. The exception to
  204. this rule is for methods that are automatically inferred from the rest
  205. of the API:</p>
  206. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">D</span><span class="p">:</span> <span class="c1"># No inheritance</span>
  207. <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="o">...</span> <span class="c1"># Extra method not required by the ABC</span>
  208. <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Abstract method</span>
  209. <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Abstract method</span>
  210. <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Mixin method</span>
  211. <span class="k">def</span> <span class="nf">index</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="o">...</span> <span class="c1"># Mixin method</span>
  212. <span class="n">Sequence</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">D</span><span class="p">)</span> <span class="c1"># Register instead of inherit</span>
  213. </pre></div>
  214. </div>
  215. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">issubclass</span><span class="p">(</span><span class="n">D</span><span class="p">,</span> <span class="n">Sequence</span><span class="p">)</span>
  216. <span class="go">True</span>
  217. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">D</span><span class="p">(),</span> <span class="n">Sequence</span><span class="p">)</span>
  218. <span class="go">True</span>
  219. </pre></div>
  220. </div>
  221. <p>In this example, class <code class="xref py py-class docutils literal notranslate"><span class="pre">D</span></code> does not need to define
  222. <code class="docutils literal notranslate"><span class="pre">__contains__</span></code>, <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>, and <code class="docutils literal notranslate"><span class="pre">__reversed__</span></code> because the
  223. <a class="reference internal" href="../reference/expressions.html#comparisons"><span class="std std-ref">in-operator</span></a>, the <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iteration</span></a>
  224. logic, and the <a class="reference internal" href="functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> function automatically fall back to
  225. using <code class="docutils literal notranslate"><span class="pre">__getitem__</span></code> and <code class="docutils literal notranslate"><span class="pre">__len__</span></code>.</p>
  226. <p>3) Some simple interfaces are directly recognizable by the presence of
  227. the required methods (unless those methods have been set to
  228. <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>
  229. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">E</span><span class="p">:</span>
  230. <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span>
  231. <span class="k">def</span> <span class="fm">__next__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="o">...</span>
  232. </pre></div>
  233. </div>
  234. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">issubclass</span><span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="n">Iterable</span><span class="p">)</span>
  235. <span class="go">True</span>
  236. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">E</span><span class="p">(),</span> <span class="n">Iterable</span><span class="p">)</span>
  237. <span class="go">True</span>
  238. </pre></div>
  239. </div>
  240. <p>Complex interfaces do not support this last technique because an
  241. interface is more than just the presence of method names. Interfaces
  242. specify semantics and relationships between methods that cannot be
  243. inferred solely from the presence of specific method names. For
  244. example, knowing that a class supplies <code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>, <code class="docutils literal notranslate"><span class="pre">__len__</span></code>, and
  245. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code> is insufficient for distinguishing a <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a> from
  246. a <a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a>.</p>
  247. <div class="versionadded">
  248. <p><span class="versionmodified added">New in version 3.9: </span>These abstract classes now support <code class="docutils literal notranslate"><span class="pre">[]</span></code>. See <a class="reference internal" href="stdtypes.html#types-genericalias"><span class="std std-ref">Generic Alias Type</span></a>
  249. and <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0585/"><strong>PEP 585</strong></a>.</p>
  250. </div>
  251. <section id="collections-abstract-base-classes">
  252. <span id="id1"></span><h2>Collections Abstract Base Classes<a class="headerlink" href="#collections-abstract-base-classes" title="Link to this heading">¶</a></h2>
  253. <p>The collections module offers the following <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">ABCs</span></a>:</p>
  254. <table class="docutils align-default">
  255. <thead>
  256. <tr class="row-odd"><th class="head"><p>ABC</p></th>
  257. <th class="head"><p>Inherits from</p></th>
  258. <th class="head"><p>Abstract Methods</p></th>
  259. <th class="head"><p>Mixin Methods</p></th>
  260. </tr>
  261. </thead>
  262. <tbody>
  263. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal notranslate"><span class="pre">Container</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id2" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  264. <td></td>
  265. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code></p></td>
  266. <td></td>
  267. </tr>
  268. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Hashable</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id3" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  269. <td></td>
  270. <td><p><code class="docutils literal notranslate"><span class="pre">__hash__</span></code></p></td>
  271. <td></td>
  272. </tr>
  273. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id4" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> <a class="footnote-reference brackets" href="#id19" id="id5" role="doc-noteref"><span class="fn-bracket">[</span>2<span class="fn-bracket">]</span></a></p></td>
  274. <td></td>
  275. <td><p><code class="docutils literal notranslate"><span class="pre">__iter__</span></code></p></td>
  276. <td></td>
  277. </tr>
  278. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Iterator" title="collections.abc.Iterator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterator</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id6" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  279. <td><p><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a></p></td>
  280. <td><p><code class="docutils literal notranslate"><span class="pre">__next__</span></code></p></td>
  281. <td><p><code class="docutils literal notranslate"><span class="pre">__iter__</span></code></p></td>
  282. </tr>
  283. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Reversible" title="collections.abc.Reversible"><code class="xref py py-class docutils literal notranslate"><span class="pre">Reversible</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id7" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  284. <td><p><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a></p></td>
  285. <td><p><code class="docutils literal notranslate"><span class="pre">__reversed__</span></code></p></td>
  286. <td></td>
  287. </tr>
  288. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id8" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  289. <td><p><a class="reference internal" href="#collections.abc.Iterator" title="collections.abc.Iterator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterator</span></code></a></p></td>
  290. <td><p><code class="docutils literal notranslate"><span class="pre">send</span></code>, <code class="docutils literal notranslate"><span class="pre">throw</span></code></p></td>
  291. <td><p><code class="docutils literal notranslate"><span class="pre">close</span></code>, <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>, <code class="docutils literal notranslate"><span class="pre">__next__</span></code></p></td>
  292. </tr>
  293. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sized</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id9" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  294. <td></td>
  295. <td><p><code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  296. <td></td>
  297. </tr>
  298. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Callable</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id10" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  299. <td></td>
  300. <td><p><code class="docutils literal notranslate"><span class="pre">__call__</span></code></p></td>
  301. <td></td>
  302. </tr>
  303. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id11" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  304. <td><p><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sized</span></code></a>,
  305. <a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a>,
  306. <a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal notranslate"><span class="pre">Container</span></code></a></p></td>
  307. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>,
  308. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  309. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  310. <td></td>
  311. </tr>
  312. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a></p></td>
  313. <td><p><a class="reference internal" href="#collections.abc.Reversible" title="collections.abc.Reversible"><code class="xref py py-class docutils literal notranslate"><span class="pre">Reversible</span></code></a>,
  314. <a class="reference internal" href="#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a></p></td>
  315. <td><p><code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>,
  316. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  317. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>, <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>, <code class="docutils literal notranslate"><span class="pre">__reversed__</span></code>,
  318. <code class="docutils literal notranslate"><span class="pre">index</span></code>, and <code class="docutils literal notranslate"><span class="pre">count</span></code></p></td>
  319. </tr>
  320. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.MutableSequence" title="collections.abc.MutableSequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableSequence</span></code></a></p></td>
  321. <td><p><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a></p></td>
  322. <td><p><code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>,
  323. <code class="docutils literal notranslate"><span class="pre">__setitem__</span></code>,
  324. <code class="docutils literal notranslate"><span class="pre">__delitem__</span></code>,
  325. <code class="docutils literal notranslate"><span class="pre">__len__</span></code>,
  326. <code class="docutils literal notranslate"><span class="pre">insert</span></code></p></td>
  327. <td><p>Inherited <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a> methods and
  328. <code class="docutils literal notranslate"><span class="pre">append</span></code>, <code class="docutils literal notranslate"><span class="pre">clear</span></code>, <code class="docutils literal notranslate"><span class="pre">reverse</span></code>, <code class="docutils literal notranslate"><span class="pre">extend</span></code>,
  329. <code class="docutils literal notranslate"><span class="pre">pop</span></code>, <code class="docutils literal notranslate"><span class="pre">remove</span></code>, and <code class="docutils literal notranslate"><span class="pre">__iadd__</span></code></p></td>
  330. </tr>
  331. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.ByteString" title="collections.abc.ByteString"><code class="xref py py-class docutils literal notranslate"><span class="pre">ByteString</span></code></a></p></td>
  332. <td><p><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a></p></td>
  333. <td><p><code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>,
  334. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  335. <td><p>Inherited <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a> methods</p></td>
  336. </tr>
  337. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a></p></td>
  338. <td><p><a class="reference internal" href="#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a></p></td>
  339. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>,
  340. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  341. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  342. <td><p><code class="docutils literal notranslate"><span class="pre">__le__</span></code>, <code class="docutils literal notranslate"><span class="pre">__lt__</span></code>, <code class="docutils literal notranslate"><span class="pre">__eq__</span></code>, <code class="docutils literal notranslate"><span class="pre">__ne__</span></code>,
  343. <code class="docutils literal notranslate"><span class="pre">__gt__</span></code>, <code class="docutils literal notranslate"><span class="pre">__ge__</span></code>, <code class="docutils literal notranslate"><span class="pre">__and__</span></code>, <code class="docutils literal notranslate"><span class="pre">__or__</span></code>,
  344. <code class="docutils literal notranslate"><span class="pre">__sub__</span></code>, <code class="docutils literal notranslate"><span class="pre">__xor__</span></code>, and <code class="docutils literal notranslate"><span class="pre">isdisjoint</span></code></p></td>
  345. </tr>
  346. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableSet</span></code></a></p></td>
  347. <td><p><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a></p></td>
  348. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>,
  349. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  350. <code class="docutils literal notranslate"><span class="pre">__len__</span></code>,
  351. <code class="docutils literal notranslate"><span class="pre">add</span></code>,
  352. <code class="docutils literal notranslate"><span class="pre">discard</span></code></p></td>
  353. <td><p>Inherited <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> methods and
  354. <code class="docutils literal notranslate"><span class="pre">clear</span></code>, <code class="docutils literal notranslate"><span class="pre">pop</span></code>, <code class="docutils literal notranslate"><span class="pre">remove</span></code>, <code class="docutils literal notranslate"><span class="pre">__ior__</span></code>,
  355. <code class="docutils literal notranslate"><span class="pre">__iand__</span></code>, <code class="docutils literal notranslate"><span class="pre">__ixor__</span></code>, and <code class="docutils literal notranslate"><span class="pre">__isub__</span></code></p></td>
  356. </tr>
  357. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a></p></td>
  358. <td><p><a class="reference internal" href="#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a></p></td>
  359. <td><p><code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>,
  360. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  361. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  362. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>, <code class="docutils literal notranslate"><span class="pre">keys</span></code>, <code class="docutils literal notranslate"><span class="pre">items</span></code>, <code class="docutils literal notranslate"><span class="pre">values</span></code>,
  363. <code class="docutils literal notranslate"><span class="pre">get</span></code>, <code class="docutils literal notranslate"><span class="pre">__eq__</span></code>, and <code class="docutils literal notranslate"><span class="pre">__ne__</span></code></p></td>
  364. </tr>
  365. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableMapping</span></code></a></p></td>
  366. <td><p><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a></p></td>
  367. <td><p><code class="docutils literal notranslate"><span class="pre">__getitem__</span></code>,
  368. <code class="docutils literal notranslate"><span class="pre">__setitem__</span></code>,
  369. <code class="docutils literal notranslate"><span class="pre">__delitem__</span></code>,
  370. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code>,
  371. <code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  372. <td><p>Inherited <a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a> methods and
  373. <code class="docutils literal notranslate"><span class="pre">pop</span></code>, <code class="docutils literal notranslate"><span class="pre">popitem</span></code>, <code class="docutils literal notranslate"><span class="pre">clear</span></code>, <code class="docutils literal notranslate"><span class="pre">update</span></code>,
  374. and <code class="docutils literal notranslate"><span class="pre">setdefault</span></code></p></td>
  375. </tr>
  376. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal notranslate"><span class="pre">MappingView</span></code></a></p></td>
  377. <td><p><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sized</span></code></a></p></td>
  378. <td></td>
  379. <td><p><code class="docutils literal notranslate"><span class="pre">__len__</span></code></p></td>
  380. </tr>
  381. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.ItemsView" title="collections.abc.ItemsView"><code class="xref py py-class docutils literal notranslate"><span class="pre">ItemsView</span></code></a></p></td>
  382. <td><p><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal notranslate"><span class="pre">MappingView</span></code></a>,
  383. <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a></p></td>
  384. <td></td>
  385. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>,
  386. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code></p></td>
  387. </tr>
  388. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.KeysView" title="collections.abc.KeysView"><code class="xref py py-class docutils literal notranslate"><span class="pre">KeysView</span></code></a></p></td>
  389. <td><p><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal notranslate"><span class="pre">MappingView</span></code></a>,
  390. <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a></p></td>
  391. <td></td>
  392. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>,
  393. <code class="docutils literal notranslate"><span class="pre">__iter__</span></code></p></td>
  394. </tr>
  395. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.ValuesView" title="collections.abc.ValuesView"><code class="xref py py-class docutils literal notranslate"><span class="pre">ValuesView</span></code></a></p></td>
  396. <td><p><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal notranslate"><span class="pre">MappingView</span></code></a>,
  397. <a class="reference internal" href="#collections.abc.Collection" title="collections.abc.Collection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Collection</span></code></a></p></td>
  398. <td></td>
  399. <td><p><code class="docutils literal notranslate"><span class="pre">__contains__</span></code>, <code class="docutils literal notranslate"><span class="pre">__iter__</span></code></p></td>
  400. </tr>
  401. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Awaitable</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id12" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  402. <td></td>
  403. <td><p><code class="docutils literal notranslate"><span class="pre">__await__</span></code></p></td>
  404. <td></td>
  405. </tr>
  406. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Coroutine</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id13" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  407. <td><p><a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Awaitable</span></code></a></p></td>
  408. <td><p><code class="docutils literal notranslate"><span class="pre">send</span></code>, <code class="docutils literal notranslate"><span class="pre">throw</span></code></p></td>
  409. <td><p><code class="docutils literal notranslate"><span class="pre">close</span></code></p></td>
  410. </tr>
  411. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.AsyncIterable" title="collections.abc.AsyncIterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterable</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id14" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  412. <td></td>
  413. <td><p><code class="docutils literal notranslate"><span class="pre">__aiter__</span></code></p></td>
  414. <td></td>
  415. </tr>
  416. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.AsyncIterator" title="collections.abc.AsyncIterator"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterator</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id15" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  417. <td><p><a class="reference internal" href="#collections.abc.AsyncIterable" title="collections.abc.AsyncIterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterable</span></code></a></p></td>
  418. <td><p><code class="docutils literal notranslate"><span class="pre">__anext__</span></code></p></td>
  419. <td><p><code class="docutils literal notranslate"><span class="pre">__aiter__</span></code></p></td>
  420. </tr>
  421. <tr class="row-even"><td><p><a class="reference internal" href="#collections.abc.AsyncGenerator" title="collections.abc.AsyncGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncGenerator</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id16" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  422. <td><p><a class="reference internal" href="#collections.abc.AsyncIterator" title="collections.abc.AsyncIterator"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterator</span></code></a></p></td>
  423. <td><p><code class="docutils literal notranslate"><span class="pre">asend</span></code>, <code class="docutils literal notranslate"><span class="pre">athrow</span></code></p></td>
  424. <td><p><code class="docutils literal notranslate"><span class="pre">aclose</span></code>, <code class="docutils literal notranslate"><span class="pre">__aiter__</span></code>, <code class="docutils literal notranslate"><span class="pre">__anext__</span></code></p></td>
  425. </tr>
  426. <tr class="row-odd"><td><p><a class="reference internal" href="#collections.abc.Buffer" title="collections.abc.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Buffer</span></code></a> <a class="footnote-reference brackets" href="#id18" id="id17" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p></td>
  427. <td></td>
  428. <td><p><code class="docutils literal notranslate"><span class="pre">__buffer__</span></code></p></td>
  429. <td></td>
  430. </tr>
  431. </tbody>
  432. </table>
  433. <p class="rubric">Footnotes</p>
  434. <aside class="footnote-list brackets">
  435. <aside class="footnote brackets" id="id18" role="doc-footnote">
  436. <span class="label"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></span>
  437. <span class="backrefs">(<a role="doc-backlink" href="#id2">1</a>,<a role="doc-backlink" href="#id3">2</a>,<a role="doc-backlink" href="#id4">3</a>,<a role="doc-backlink" href="#id6">4</a>,<a role="doc-backlink" href="#id7">5</a>,<a role="doc-backlink" href="#id8">6</a>,<a role="doc-backlink" href="#id9">7</a>,<a role="doc-backlink" href="#id10">8</a>,<a role="doc-backlink" href="#id11">9</a>,<a role="doc-backlink" href="#id12">10</a>,<a role="doc-backlink" href="#id13">11</a>,<a role="doc-backlink" href="#id14">12</a>,<a role="doc-backlink" href="#id15">13</a>,<a role="doc-backlink" href="#id16">14</a>,<a role="doc-backlink" href="#id17">15</a>)</span>
  438. <p>These ABCs override <a class="reference internal" href="abc.html#abc.ABCMeta.__subclasshook__" title="abc.ABCMeta.__subclasshook__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__subclasshook__()</span></code></a> to support
  439. testing an interface by verifying the required methods are present
  440. and have not been set 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>. This only works for simple
  441. interfaces. More complex interfaces require registration or direct
  442. subclassing.</p>
  443. </aside>
  444. <aside class="footnote brackets" id="id19" role="doc-footnote">
  445. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id5">2</a><span class="fn-bracket">]</span></span>
  446. <p>Checking <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">Iterable)</span></code> detects classes that are
  447. registered as <a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a> or that have an <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a>
  448. method, but it does not detect classes that iterate with the
  449. <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method. The only reliable way to determine
  450. whether an object is <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> is to call <code class="docutils literal notranslate"><span class="pre">iter(obj)</span></code>.</p>
  451. </aside>
  452. </aside>
  453. </section>
  454. <section id="collections-abstract-base-classes-detailed-descriptions">
  455. <h2>Collections Abstract Base Classes – Detailed Descriptions<a class="headerlink" href="#collections-abstract-base-classes-detailed-descriptions" title="Link to this heading">¶</a></h2>
  456. <dl class="py class">
  457. <dt class="sig sig-object py" id="collections.abc.Container">
  458. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Container</span></span><a class="headerlink" href="#collections.abc.Container" title="Link to this definition">¶</a></dt>
  459. <dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a> method.</p>
  460. </dd></dl>
  461. <dl class="py class">
  462. <dt class="sig sig-object py" id="collections.abc.Hashable">
  463. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Hashable</span></span><a class="headerlink" href="#collections.abc.Hashable" title="Link to this definition">¶</a></dt>
  464. <dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> method.</p>
  465. </dd></dl>
  466. <dl class="py class">
  467. <dt class="sig sig-object py" id="collections.abc.Sized">
  468. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Sized</span></span><a class="headerlink" href="#collections.abc.Sized" title="Link to this definition">¶</a></dt>
  469. <dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a> method.</p>
  470. </dd></dl>
  471. <dl class="py class">
  472. <dt class="sig sig-object py" id="collections.abc.Callable">
  473. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Callable</span></span><a class="headerlink" href="#collections.abc.Callable" title="Link to this definition">¶</a></dt>
  474. <dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> method.</p>
  475. </dd></dl>
  476. <dl class="py class">
  477. <dt class="sig sig-object py" id="collections.abc.Iterable">
  478. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Iterable</span></span><a class="headerlink" href="#collections.abc.Iterable" title="Link to this definition">¶</a></dt>
  479. <dd><p>ABC for classes that provide the <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> method.</p>
  480. <p>Checking <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">Iterable)</span></code> detects classes that are registered
  481. as <a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Iterable</span></code></a> or that have an <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> method,
  482. but it does
  483. not detect classes that iterate with the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method.
  484. The only reliable way to determine whether an object is <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>
  485. is to call <code class="docutils literal notranslate"><span class="pre">iter(obj)</span></code>.</p>
  486. </dd></dl>
  487. <dl class="py class">
  488. <dt class="sig sig-object py" id="collections.abc.Collection">
  489. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Collection</span></span><a class="headerlink" href="#collections.abc.Collection" title="Link to this definition">¶</a></dt>
  490. <dd><p>ABC for sized iterable container classes.</p>
  491. <div class="versionadded">
  492. <p><span class="versionmodified added">New in version 3.6.</span></p>
  493. </div>
  494. </dd></dl>
  495. <dl class="py class">
  496. <dt class="sig sig-object py" id="collections.abc.Iterator">
  497. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Iterator</span></span><a class="headerlink" href="#collections.abc.Iterator" title="Link to this definition">¶</a></dt>
  498. <dd><p>ABC for classes that provide the <a class="reference internal" href="stdtypes.html#iterator.__iter__" title="iterator.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> and
  499. <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code></a> methods. See also the definition of
  500. <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>.</p>
  501. </dd></dl>
  502. <dl class="py class">
  503. <dt class="sig sig-object py" id="collections.abc.Reversible">
  504. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Reversible</span></span><a class="headerlink" href="#collections.abc.Reversible" title="Link to this definition">¶</a></dt>
  505. <dd><p>ABC for iterable classes that also provide the <a class="reference internal" href="../reference/datamodel.html#object.__reversed__" title="object.__reversed__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__reversed__()</span></code></a>
  506. method.</p>
  507. <div class="versionadded">
  508. <p><span class="versionmodified added">New in version 3.6.</span></p>
  509. </div>
  510. </dd></dl>
  511. <dl class="py class">
  512. <dt class="sig sig-object py" id="collections.abc.Generator">
  513. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Generator</span></span><a class="headerlink" href="#collections.abc.Generator" title="Link to this definition">¶</a></dt>
  514. <dd><p>ABC for <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> classes that implement the protocol defined in
  515. <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0342/"><strong>PEP 342</strong></a> that extends <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterators</span></a> with the
  516. <a class="reference internal" href="../reference/expressions.html#generator.send" title="generator.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a>,
  517. <a class="reference internal" href="../reference/expressions.html#generator.throw" title="generator.throw"><code class="xref py py-meth docutils literal notranslate"><span class="pre">throw()</span></code></a> and <a class="reference internal" href="../reference/expressions.html#generator.close" title="generator.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> methods.</p>
  518. <div class="versionadded">
  519. <p><span class="versionmodified added">New in version 3.5.</span></p>
  520. </div>
  521. </dd></dl>
  522. <dl class="py class">
  523. <dt class="sig sig-object py" id="collections.abc.Sequence">
  524. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Sequence</span></span><a class="headerlink" href="#collections.abc.Sequence" title="Link to this definition">¶</a></dt>
  525. <dt class="sig sig-object py" id="collections.abc.MutableSequence">
  526. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">MutableSequence</span></span><a class="headerlink" href="#collections.abc.MutableSequence" title="Link to this definition">¶</a></dt>
  527. <dt class="sig sig-object py" id="collections.abc.ByteString">
  528. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">ByteString</span></span><a class="headerlink" href="#collections.abc.ByteString" title="Link to this definition">¶</a></dt>
  529. <dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequences</span></a>.</p>
  530. <p>Implementation note: Some of the mixin methods, such as
  531. <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__reversed__" title="object.__reversed__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__reversed__()</span></code></a> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">index()</span></code>, make
  532. repeated calls to the underlying <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> method.
  533. Consequently, if <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> is implemented with constant
  534. access speed, the mixin methods will have linear performance;
  535. however, if the underlying method is linear (as it would be with a
  536. linked list), the mixins will have quadratic performance and will
  537. likely need to be overridden.</p>
  538. <div class="versionchanged">
  539. <p><span class="versionmodified changed">Changed in version 3.5: </span>The index() method added support for <em>stop</em> and <em>start</em>
  540. arguments.</p>
  541. </div>
  542. <div class="deprecated-removed">
  543. <p><span class="versionmodified">Deprecated since version 3.12, will be removed in version 3.14: </span>The <a class="reference internal" href="#collections.abc.ByteString" title="collections.abc.ByteString"><code class="xref py py-class docutils literal notranslate"><span class="pre">ByteString</span></code></a> ABC has been deprecated.
  544. For use in typing, prefer a union, like <code class="docutils literal notranslate"><span class="pre">bytes</span> <span class="pre">|</span> <span class="pre">bytearray</span></code>, or
  545. <a class="reference internal" href="#collections.abc.Buffer" title="collections.abc.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Buffer</span></code></a>.
  546. For use as an ABC, prefer <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a> or <a class="reference internal" href="#collections.abc.Buffer" title="collections.abc.Buffer"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Buffer</span></code></a>.</p>
  547. </div>
  548. </dd></dl>
  549. <dl class="py class">
  550. <dt class="sig sig-object py" id="collections.abc.Set">
  551. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Set</span></span><a class="headerlink" href="#collections.abc.Set" title="Link to this definition">¶</a></dt>
  552. <dt class="sig sig-object py" id="collections.abc.MutableSet">
  553. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">MutableSet</span></span><a class="headerlink" href="#collections.abc.MutableSet" title="Link to this definition">¶</a></dt>
  554. <dd><p>ABCs for read-only and mutable <a class="reference internal" href="stdtypes.html#types-set"><span class="std std-ref">sets</span></a>.</p>
  555. </dd></dl>
  556. <dl class="py class">
  557. <dt class="sig sig-object py" id="collections.abc.Mapping">
  558. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Mapping</span></span><a class="headerlink" href="#collections.abc.Mapping" title="Link to this definition">¶</a></dt>
  559. <dt class="sig sig-object py" id="collections.abc.MutableMapping">
  560. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">MutableMapping</span></span><a class="headerlink" href="#collections.abc.MutableMapping" title="Link to this definition">¶</a></dt>
  561. <dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mappings</span></a>.</p>
  562. </dd></dl>
  563. <dl class="py class">
  564. <dt class="sig sig-object py" id="collections.abc.MappingView">
  565. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">MappingView</span></span><a class="headerlink" href="#collections.abc.MappingView" title="Link to this definition">¶</a></dt>
  566. <dt class="sig sig-object py" id="collections.abc.ItemsView">
  567. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">ItemsView</span></span><a class="headerlink" href="#collections.abc.ItemsView" title="Link to this definition">¶</a></dt>
  568. <dt class="sig sig-object py" id="collections.abc.KeysView">
  569. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">KeysView</span></span><a class="headerlink" href="#collections.abc.KeysView" title="Link to this definition">¶</a></dt>
  570. <dt class="sig sig-object py" id="collections.abc.ValuesView">
  571. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">ValuesView</span></span><a class="headerlink" href="#collections.abc.ValuesView" title="Link to this definition">¶</a></dt>
  572. <dd><p>ABCs for mapping, items, keys, and values <a class="reference internal" href="../glossary.html#term-dictionary-view"><span class="xref std std-term">views</span></a>.</p>
  573. </dd></dl>
  574. <dl class="py class">
  575. <dt class="sig sig-object py" id="collections.abc.Awaitable">
  576. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Awaitable</span></span><a class="headerlink" href="#collections.abc.Awaitable" title="Link to this definition">¶</a></dt>
  577. <dd><p>ABC for <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> objects, which can be used in <a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a>
  578. expressions. Custom implementations must provide the
  579. <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> method.</p>
  580. <p><a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">Coroutine</span></a> objects and instances of the
  581. <a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Coroutine</span></code></a> ABC are all instances of this ABC.</p>
  582. <div class="admonition note">
  583. <p class="admonition-title">Note</p>
  584. <p>In CPython, generator-based coroutines (<a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generators</span></a>
  585. decorated with <a class="reference internal" href="types.html#types.coroutine" title="types.coroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;types.coroutine</span></code></a>) are
  586. <em>awaitables</em>, even though they do not have an <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> method.
  587. Using <code class="docutils literal notranslate"><span class="pre">isinstance(gencoro,</span> <span class="pre">Awaitable)</span></code> for them will return <code class="docutils literal notranslate"><span class="pre">False</span></code>.
  588. Use <a class="reference internal" href="inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.isawaitable()</span></code></a> to detect them.</p>
  589. </div>
  590. <div class="versionadded">
  591. <p><span class="versionmodified added">New in version 3.5.</span></p>
  592. </div>
  593. </dd></dl>
  594. <dl class="py class">
  595. <dt class="sig sig-object py" id="collections.abc.Coroutine">
  596. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Coroutine</span></span><a class="headerlink" href="#collections.abc.Coroutine" title="Link to this definition">¶</a></dt>
  597. <dd><p>ABC for <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> compatible classes. These implement the
  598. following methods, defined in <a class="reference internal" href="../reference/datamodel.html#coroutine-objects"><span class="std std-ref">Coroutine Objects</span></a>:
  599. <a class="reference internal" href="../reference/datamodel.html#coroutine.send" title="coroutine.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#coroutine.throw" title="coroutine.throw"><code class="xref py py-meth docutils literal notranslate"><span class="pre">throw()</span></code></a>, and
  600. <a class="reference internal" href="../reference/datamodel.html#coroutine.close" title="coroutine.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a>. Custom implementations must also implement
  601. <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a>. All <a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Coroutine</span></code></a> instances are also
  602. instances of <a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Awaitable</span></code></a>.</p>
  603. <div class="admonition note">
  604. <p class="admonition-title">Note</p>
  605. <p>In CPython, generator-based coroutines (<a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generators</span></a>
  606. decorated with <a class="reference internal" href="types.html#types.coroutine" title="types.coroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">&#64;types.coroutine</span></code></a>) are
  607. <em>awaitables</em>, even though they do not have an <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> method.
  608. Using <code class="docutils literal notranslate"><span class="pre">isinstance(gencoro,</span> <span class="pre">Coroutine)</span></code> for them will return <code class="docutils literal notranslate"><span class="pre">False</span></code>.
  609. Use <a class="reference internal" href="inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.isawaitable()</span></code></a> to detect them.</p>
  610. </div>
  611. <div class="versionadded">
  612. <p><span class="versionmodified added">New in version 3.5.</span></p>
  613. </div>
  614. </dd></dl>
  615. <dl class="py class">
  616. <dt class="sig sig-object py" id="collections.abc.AsyncIterable">
  617. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">AsyncIterable</span></span><a class="headerlink" href="#collections.abc.AsyncIterable" title="Link to this definition">¶</a></dt>
  618. <dd><p>ABC for classes that provide an <code class="docutils literal notranslate"><span class="pre">__aiter__</span></code> method. See also the
  619. definition of <a class="reference internal" href="../glossary.html#term-asynchronous-iterable"><span class="xref std std-term">asynchronous iterable</span></a>.</p>
  620. <div class="versionadded">
  621. <p><span class="versionmodified added">New in version 3.5.</span></p>
  622. </div>
  623. </dd></dl>
  624. <dl class="py class">
  625. <dt class="sig sig-object py" id="collections.abc.AsyncIterator">
  626. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">AsyncIterator</span></span><a class="headerlink" href="#collections.abc.AsyncIterator" title="Link to this definition">¶</a></dt>
  627. <dd><p>ABC for classes that provide <code class="docutils literal notranslate"><span class="pre">__aiter__</span></code> and <code class="docutils literal notranslate"><span class="pre">__anext__</span></code>
  628. methods. See also the definition of <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a>.</p>
  629. <div class="versionadded">
  630. <p><span class="versionmodified added">New in version 3.5.</span></p>
  631. </div>
  632. </dd></dl>
  633. <dl class="py class">
  634. <dt class="sig sig-object py" id="collections.abc.AsyncGenerator">
  635. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">AsyncGenerator</span></span><a class="headerlink" href="#collections.abc.AsyncGenerator" title="Link to this definition">¶</a></dt>
  636. <dd><p>ABC for <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> classes that implement the protocol
  637. defined in <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0525/"><strong>PEP 525</strong></a> and <span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0492/"><strong>PEP 492</strong></a>.</p>
  638. <div class="versionadded">
  639. <p><span class="versionmodified added">New in version 3.6.</span></p>
  640. </div>
  641. </dd></dl>
  642. <dl class="py class">
  643. <dt class="sig sig-object py" id="collections.abc.Buffer">
  644. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">collections.abc.</span></span><span class="sig-name descname"><span class="pre">Buffer</span></span><a class="headerlink" href="#collections.abc.Buffer" title="Link to this definition">¶</a></dt>
  645. <dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__buffer__" title="object.__buffer__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__buffer__()</span></code></a> method,
  646. implementing the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer protocol</span></a>. See <span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0688/"><strong>PEP 688</strong></a>.</p>
  647. <div class="versionadded">
  648. <p><span class="versionmodified added">New in version 3.12.</span></p>
  649. </div>
  650. </dd></dl>
  651. </section>
  652. <section id="examples-and-recipes">
  653. <h2>Examples and Recipes<a class="headerlink" href="#examples-and-recipes" title="Link to this heading">¶</a></h2>
  654. <p>ABCs allow us to ask classes or instances if they provide
  655. particular functionality, for example:</p>
  656. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">size</span> <span class="o">=</span> <span class="kc">None</span>
  657. <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">myvar</span><span class="p">,</span> <span class="n">collections</span><span class="o">.</span><span class="n">abc</span><span class="o">.</span><span class="n">Sized</span><span class="p">):</span>
  658. <span class="n">size</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">myvar</span><span class="p">)</span>
  659. </pre></div>
  660. </div>
  661. <p>Several of the ABCs are also useful as mixins that make it easier to develop
  662. classes supporting container APIs. For example, to write a class supporting
  663. the full <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> API, it is only necessary to supply the three underlying
  664. abstract methods: <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a>, <a class="reference internal" href="stdtypes.html#container.__iter__" title="container.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a>, and
  665. <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a>. The ABC supplies the remaining methods such as
  666. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__and__()</span></code> and <a class="reference internal" href="stdtypes.html#frozenset.isdisjoint" title="frozenset.isdisjoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isdisjoint()</span></code></a>:</p>
  667. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ListBasedSet</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">abc</span><span class="o">.</span><span class="n">Set</span><span class="p">):</span>
  668. <span class="w"> </span><span class="sd">&#39;&#39;&#39; Alternate set implementation favoring space over speed</span>
  669. <span class="sd"> and not requiring the set elements to be hashable. &#39;&#39;&#39;</span>
  670. <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">iterable</span><span class="p">):</span>
  671. <span class="bp">self</span><span class="o">.</span><span class="n">elements</span> <span class="o">=</span> <span class="n">lst</span> <span class="o">=</span> <span class="p">[]</span>
  672. <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
  673. <span class="k">if</span> <span class="n">value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">lst</span><span class="p">:</span>
  674. <span class="n">lst</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  675. <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  676. <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
  677. <span class="k">def</span> <span class="fm">__contains__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
  678. <span class="k">return</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">elements</span>
  679. <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  680. <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">elements</span><span class="p">)</span>
  681. <span class="n">s1</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s1">&#39;abcdef&#39;</span><span class="p">)</span>
  682. <span class="n">s2</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s1">&#39;defghi&#39;</span><span class="p">)</span>
  683. <span class="n">overlap</span> <span class="o">=</span> <span class="n">s1</span> <span class="o">&amp;</span> <span class="n">s2</span> <span class="c1"># The __and__() method is supported automatically</span>
  684. </pre></div>
  685. </div>
  686. <p>Notes on using <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> and <a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableSet</span></code></a> as a mixin:</p>
  687. <ol class="arabic simple">
  688. <li><p>Since some set operations create new sets, the default mixin methods need
  689. a way to create new instances from an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>. The class constructor is
  690. assumed to have a signature in the form <code class="docutils literal notranslate"><span class="pre">ClassName(iterable)</span></code>.
  691. That assumption is factored-out to an internal <a class="reference internal" href="functions.html#classmethod" title="classmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">classmethod</span></code></a> called
  692. <code class="xref py py-meth docutils literal notranslate"><span class="pre">_from_iterable()</span></code> which calls <code class="docutils literal notranslate"><span class="pre">cls(iterable)</span></code> to produce a new set.
  693. If the <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> mixin is being used in a class with a different
  694. constructor signature, you will need to override <code class="xref py py-meth docutils literal notranslate"><span class="pre">_from_iterable()</span></code>
  695. with a classmethod or regular method that can construct new instances from
  696. an iterable argument.</p></li>
  697. <li><p>To override the comparisons (presumably for speed, as the
  698. semantics are fixed), redefine <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code></a> and
  699. <a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code></a>,
  700. then the other operations will automatically follow suit.</p></li>
  701. <li><p>The <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> mixin provides a <code class="xref py py-meth docutils literal notranslate"><span class="pre">_hash()</span></code> method to compute a hash value
  702. for the set; however, <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__hash__()</span></code></a> is not defined because not all sets
  703. are <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> or immutable. To add set hashability using mixins,
  704. inherit from both <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Set()</span></code></a> and <a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Hashable()</span></code></a>, then define
  705. <code class="docutils literal notranslate"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">Set._hash</span></code>.</p></li>
  706. </ol>
  707. <div class="admonition seealso">
  708. <p class="admonition-title">See also</p>
  709. <ul class="simple">
  710. <li><p><a class="reference external" href="https://code.activestate.com/recipes/576694/">OrderedSet recipe</a> for an
  711. example built on <a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableSet</span></code></a>.</p></li>
  712. <li><p>For more about ABCs, see the <a class="reference internal" href="abc.html#module-abc" title="abc: Abstract base classes according to :pep:`3119`."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module and <span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-3119/"><strong>PEP 3119</strong></a>.</p></li>
  713. </ul>
  714. </div>
  715. </section>
  716. </section>
  717. <div class="clearer"></div>
  718. </div>
  719. </div>
  720. </div>
  721. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  722. <div class="sphinxsidebarwrapper">
  723. <div>
  724. <h3><a href="../contents.html">Table of Contents</a></h3>
  725. <ul>
  726. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code> — Abstract Base Classes for Containers</a><ul>
  727. <li><a class="reference internal" href="#collections-abstract-base-classes">Collections Abstract Base Classes</a></li>
  728. <li><a class="reference internal" href="#collections-abstract-base-classes-detailed-descriptions">Collections Abstract Base Classes – Detailed Descriptions</a></li>
  729. <li><a class="reference internal" href="#examples-and-recipes">Examples and Recipes</a></li>
  730. </ul>
  731. </li>
  732. </ul>
  733. </div>
  734. <div>
  735. <h4>Previous topic</h4>
  736. <p class="topless"><a href="collections.html"
  737. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code> — Container datatypes</a></p>
  738. </div>
  739. <div>
  740. <h4>Next topic</h4>
  741. <p class="topless"><a href="heapq.html"
  742. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">heapq</span></code> — Heap queue algorithm</a></p>
  743. </div>
  744. <div role="note" aria-label="source link">
  745. <h3>This Page</h3>
  746. <ul class="this-page-menu">
  747. <li><a href="../bugs.html">Report a Bug</a></li>
  748. <li>
  749. <a href="https://github.com/python/cpython/blob/main/Doc/library/collections.abc.rst"
  750. rel="nofollow">Show Source
  751. </a>
  752. </li>
  753. </ul>
  754. </div>
  755. </div>
  756. <div id="sidebarbutton" title="Collapse sidebar">
  757. <span>«</span>
  758. </div>
  759. </div>
  760. <div class="clearer"></div>
  761. </div>
  762. <div class="related" role="navigation" aria-label="related navigation">
  763. <h3>Navigation</h3>
  764. <ul>
  765. <li class="right" style="margin-right: 10px">
  766. <a href="../genindex.html" title="General Index"
  767. >index</a></li>
  768. <li class="right" >
  769. <a href="../py-modindex.html" title="Python Module Index"
  770. >modules</a> |</li>
  771. <li class="right" >
  772. <a href="heapq.html" title="heapq — Heap queue algorithm"
  773. >next</a> |</li>
  774. <li class="right" >
  775. <a href="collections.html" title="collections — Container datatypes"
  776. >previous</a> |</li>
  777. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  778. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  779. <li class="switchers">
  780. <div class="language_switcher_placeholder"></div>
  781. <div class="version_switcher_placeholder"></div>
  782. </li>
  783. <li>
  784. </li>
  785. <li id="cpython-language-and-version">
  786. <a href="../index.html">3.12.3 Documentation</a> &#187;
  787. </li>
  788. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  789. <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> &#187;</li>
  790. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code> — Abstract Base Classes for Containers</a></li>
  791. <li class="right">
  792. <div class="inline-search" role="search">
  793. <form class="inline-search" action="../search.html" method="get">
  794. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  795. <input type="submit" value="Go" />
  796. </form>
  797. </div>
  798. |
  799. </li>
  800. <li class="right">
  801. <label class="theme-selector-label">
  802. Theme
  803. <select class="theme-selector" oninput="activateTheme(this.value)">
  804. <option value="auto" selected>Auto</option>
  805. <option value="light">Light</option>
  806. <option value="dark">Dark</option>
  807. </select>
  808. </label> |</li>
  809. </ul>
  810. </div>
  811. <div class="footer">
  812. &copy;
  813. <a href="../copyright.html">
  814. Copyright
  815. </a>
  816. 2001-2024, Python Software Foundation.
  817. <br />
  818. This page is licensed under the Python Software Foundation License Version 2.
  819. <br />
  820. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  821. <br />
  822. See <a href="/license.html">History and License</a> for more information.<br />
  823. <br />
  824. The Python Software Foundation is a non-profit corporation.
  825. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  826. <br />
  827. <br />
  828. Last updated on Apr 09, 2024 (13:47 UTC).
  829. <a href="/bugs.html">Found a bug</a>?
  830. <br />
  831. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  832. </div>
  833. </body>
  834. </html>
上海开阖软件有限公司 沪ICP备12045867号-1