mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
25 lines
48 KiB
HTML
25 lines
48 KiB
HTML
<div class="body" role="main"><div class="section" id="module-collections.abc"><h1><span class="yiyi-st" id="yiyi-10">8.4. <a class="reference internal" href="#module-collections.abc" title="collections.abc: Abstract base classes for containers"><code class="xref py py-mod docutils literal"><span class="pre">collections.abc</span></code></a> —— 容器的抽象基类</span></h1><div class="versionadded"><p><span class="yiyi-st" id="yiyi-11"><span class="versionmodified">版本3.3中的新功能:</span>以前,此模块是<a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a>模块的一部分。</span></p></div><p><span class="yiyi-st" id="yiyi-12"><strong>源码位置:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/_collections_abc.py">Lib/_collections_abc.py</a></span></p><p><span class="yiyi-st" id="yiyi-13">此模块提供<a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">抽象基类</span></a>,可用于测试类是否提供特定接口;例如,它是否是哈希表或者它是否是映射。</span></p><div class="section" id="collections-abstract-base-classes"><h2><span class="yiyi-st" id="yiyi-14">8.4.1.</span><span class="yiyi-st" id="yiyi-15">容器抽象基类</span></h2><p><span class="yiyi-st" id="yiyi-16">collections模块提供以下<a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">抽象基类</span></a>:</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-17">抽象基类</span></th><th class="head"><span class="yiyi-st" id="yiyi-18">继承自</span></th><th class="head"><span class="yiyi-st" id="yiyi-19">抽象的方法</span></th><th class="head"><span class="yiyi-st" id="yiyi-20">Mixin方法</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-21"><a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal"><span class="pre">Container</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-22"><code class="docutils literal"><span class="pre">__contains__</span></code></span></td><td></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-23"><a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><code class="xref py py-class docutils literal"><span class="pre">Hashable</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-24"><code class="docutils literal"><span class="pre">__hash__</span></code></span></td><td></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-25"><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal"><span class="pre">Iterable</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-26"><code class="docutils literal"><span class="pre">__iter__</span></code></span></td><td></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-27"><a class="reference internal" href="#collections.abc.Iterator" title="collections.abc.Iterator"><code class="xref py py-class docutils literal"><span class="pre">Iterator</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-28"><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal"><span class="pre">Iterable</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-29"><code class="docutils literal"><span class="pre">__next__</span></code></span></td><td><span class="yiyi-st" id="yiyi-30"><code class="docutils literal"><span class="pre">__iter__</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-31"><a class="reference internal" href="#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal"><span class="pre">Generator</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-32"><a class="reference internal" href="#collections.abc.Iterator" title="collections.abc.Iterator"><code class="xref py py-class docutils literal"><span class="pre">Iterator</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-33"><code class="docutils literal"><span class="pre">send</span></code>,<code class="docutils literal"><span class="pre">throw</span></code></span></td><td><span class="yiyi-st" id="yiyi-34"><code class="docutils literal"><span class="pre">close</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__next__</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-35"><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal"><span class="pre">Sized</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-36"><code class="docutils literal"><span class="pre">__len__</span></code></span></td><td></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-37"><a class="reference internal" href="#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal"><span class="pre">Callable</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-38"><code class="docutils literal"><span class="pre">__call__</span></code></span></td><td></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-39"><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-40"><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal"><span class="pre">Sized</span></code></a>,<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal"><span class="pre">Iterable</span></code></a>,<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal"><span class="pre">Container</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-41"><code class="docutils literal"><span class="pre">__getitem__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code></span></td><td><span class="yiyi-st" id="yiyi-42"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__reversed__</span></code>,<code class="docutils literal"><span class="pre">index</span></code>和<code class="docutils literal"><span class="pre">count</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-43"><a class="reference internal" href="#collections.abc.MutableSequence" title="collections.abc.MutableSequence"><code class="xref py py-class docutils literal"><span class="pre">MutableSequence</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-44"><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-45"><code class="docutils literal"><span class="pre">__getitem__</span></code>, <code class="docutils literal"><span class="pre">__setitem__</span></code>, <code class="docutils literal"><span class="pre">__delitem__</span></code>, <code class="docutils literal"><span class="pre">__len__</span></code>, <code class="docutils literal"><span class="pre">insert</span></code></span></td><td><span class="yiyi-st" id="yiyi-46">Inherited <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a> methods and <code class="docutils literal"><span class="pre">append</span></code>, <code class="docutils literal"><span class="pre">reverse</span></code>, <code class="docutils literal"><span class="pre">extend</span></code>, <code class="docutils literal"><span class="pre">pop</span></code>, <code class="docutils literal"><span class="pre">remove</span></code>, and <code class="docutils literal"><span class="pre">__iadd__</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-47"><a class="reference internal" href="#collections.abc.ByteString" title="collections.abc.ByteString"><code class="xref py py-class docutils literal"><span class="pre">ByteString</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-48"><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-49"><code class="docutils literal"><span class="pre">__getitem__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code></span></td><td><span class="yiyi-st" id="yiyi-50">继承<a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal"><span class="pre">Sequence</span></code></a>方法</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-51"><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-52"><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal"><span class="pre">Sized</span></code></a>,<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal"><span class="pre">Iterable</span></code></a>,<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal"><span class="pre">Container</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-53"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code></span></td><td><span class="yiyi-st" id="yiyi-54"><code class="docutils literal"><span class="pre">__le__</span></code>, <code class="docutils literal"><span class="pre">__lt__</span></code>, <code class="docutils literal"><span class="pre">__eq__</span></code>, <code class="docutils literal"><span class="pre">__ne__</span></code>, <code class="docutils literal"><span class="pre">__gt__</span></code>, <code class="docutils literal"><span class="pre">__ge__</span></code>, <code class="docutils literal"><span class="pre">__and__</span></code>, <code class="docutils literal"><span class="pre">__or__</span></code>, <code class="docutils literal"><span class="pre">__sub__</span></code>, <code class="docutils literal"><span class="pre">__xor__</span></code>, and <code class="docutils literal"><span class="pre">isdisjoint</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-55"><a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal"><span class="pre">MutableSet</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-56"><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-57"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code>,<code class="docutils literal"><span class="pre">add</span></code>,<code class="docutils literal"><span class="pre">discard</span></code></span></td><td><span class="yiyi-st" id="yiyi-58">Inherited <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> methods and <code class="docutils literal"><span class="pre">clear</span></code>, <code class="docutils literal"><span class="pre">pop</span></code>, <code class="docutils literal"><span class="pre">remove</span></code>, <code class="docutils literal"><span class="pre">__ior__</span></code>, <code class="docutils literal"><span class="pre">__iand__</span></code>, <code class="docutils literal"><span class="pre">__ixor__</span></code>, and <code class="docutils literal"><span class="pre">__isub__</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-59"><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal"><span class="pre">Mapping</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-60"><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal"><span class="pre">Sized</span></code></a>,<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal"><span class="pre">Iterable</span></code></a>,<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><code class="xref py py-class docutils literal"><span class="pre">Container</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-61"><code class="docutils literal"><span class="pre">__getitem__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code></span></td><td><span class="yiyi-st" id="yiyi-62"><code class="docutils literal"><span class="pre">__contains__</span></code>, <code class="docutils literal"><span class="pre">keys</span></code>, <code class="docutils literal"><span class="pre">items</span></code>, <code class="docutils literal"><span class="pre">values</span></code>, <code class="docutils literal"><span class="pre">get</span></code>, <code class="docutils literal"><span class="pre">__eq__</span></code>, and <code class="docutils literal"><span class="pre">__ne__</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-63"><a class="reference internal" href="#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal"><span class="pre">MutableMapping</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-64"><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal"><span class="pre">Mapping</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-65"><code class="docutils literal"><span class="pre">__getitem__</span></code>,<code class="docutils literal"><span class="pre">__setitem__</span></code>,<code class="docutils literal"><span class="pre">__delitem__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code>,<code class="docutils literal"><span class="pre">__len__</span></code></span></td><td><span class="yiyi-st" id="yiyi-66">继承<a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal"><span class="pre">Mapping</span></code></a>方法和<code class="docutils literal"><span class="pre">pop</span></code>,<code class="docutils literal"><span class="pre">popitem</span></code>,<code class="docutils literal"><span class="pre">clear</span></code>,<code class="docutils literal"><span class="pre">update</span></code>和<code class="docutils literal"><span class="pre">setdefault</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-67"><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal"><span class="pre">MappingView</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-68"><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><code class="xref py py-class docutils literal"><span class="pre">Sized</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-69"><code class="docutils literal"><span class="pre">__len__</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-70"><a class="reference internal" href="#collections.abc.ItemsView" title="collections.abc.ItemsView"><code class="xref py py-class docutils literal"><span class="pre">ItemsView</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-71"><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal"><span class="pre">MappingView</span></code></a>,<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-72"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-73"><a class="reference internal" href="#collections.abc.KeysView" title="collections.abc.KeysView"><code class="xref py py-class docutils literal"><span class="pre">KeysView</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-74"><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal"><span class="pre">MappingView</span></code></a>,<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-75"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code></span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-76"><a class="reference internal" href="#collections.abc.ValuesView" title="collections.abc.ValuesView"><code class="xref py py-class docutils literal"><span class="pre">ValuesView</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-77"><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><code class="xref py py-class docutils literal"><span class="pre">MappingView</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-78"><code class="docutils literal"><span class="pre">__contains__</span></code>,<code class="docutils literal"><span class="pre">__iter__</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-79"><a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal"><span class="pre">Awaitable</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-80"><code class="docutils literal"><span class="pre">__await__</span></code></span></td><td></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-81"><a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal"><span class="pre">Coroutine</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-82"><a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal"><span class="pre">Awaitable</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-83"><code class="docutils literal"><span class="pre">send</span></code>,<code class="docutils literal"><span class="pre">throw</span></code></span></td><td><span class="yiyi-st" id="yiyi-84"><code class="docutils literal"><span class="pre">close</span></code></span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-85"><a class="reference internal" href="#collections.abc.AsyncIterable" title="collections.abc.AsyncIterable"><code class="xref py py-class docutils literal"><span class="pre">AsyncIterable</span></code></a></span></td><td></td><td><span class="yiyi-st" id="yiyi-86"><code class="docutils literal"><span class="pre">__aiter__</span></code></span></td><td></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-87"><a class="reference internal" href="#collections.abc.AsyncIterator" title="collections.abc.AsyncIterator"><code class="xref py py-class docutils literal"><span class="pre">AsyncIterator</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-88"><a class="reference internal" href="#collections.abc.AsyncIterable" title="collections.abc.AsyncIterable"><code class="xref py py-class docutils literal"><span class="pre">AsyncIterable</span></code></a></span></td><td><span class="yiyi-st" id="yiyi-89"><code class="docutils literal"><span class="pre">__anext__</span></code></span></td><td><span class="yiyi-st" id="yiyi-90"><code class="docutils literal"><span class="pre">__aiter__</span></code></span></td></tr></tbody></table><dl class="class"><dt id="collections.abc.Container"><span class="yiyi-st" id="yiyi-91"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Container</code></span></dt><dt id="collections.abc.Hashable"><span class="yiyi-st" id="yiyi-92"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Hashable</code></span></dt><dt id="collections.abc.Sized"><span class="yiyi-st" id="yiyi-93"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Sized</code></span></dt><dt id="collections.abc.Callable"><span class="yiyi-st" id="yiyi-94"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Callable</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-95">分别为类提供<a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></code></a>、<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a>、<a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal"><span class="pre">__len__()</span></code></a>和<a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal"><span class="pre">__call__()</span></code></a>方法的抽象基类。</span></p></dd></dl><dl class="class"><dt id="collections.abc.Iterable"><span class="yiyi-st" id="yiyi-96"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Iterable</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-97">提供<a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></code></a>方法的类的ABC。</span><span class="yiyi-st" id="yiyi-98">另请参见<a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a>的定义。</span></p></dd></dl><dl class="class"><dt id="collections.abc.Iterator"><span class="yiyi-st" id="yiyi-99"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Iterator</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-100">提供<a class="reference internal" href="stdtypes.html#iterator.__iter__" title="iterator.__iter__"><code class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></code></a>和<a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><code class="xref py py-meth docutils literal"><span class="pre">__next__()</span></code></a>方法的类的ABC。</span><span class="yiyi-st" id="yiyi-101">另请参见<a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>的定义。</span></p></dd></dl><dl class="class"><dt id="collections.abc.Generator"><span class="yiyi-st" id="yiyi-102"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Generator</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-103">ABC for generator classes that implement the protocol defined in <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0342"><strong>PEP 342</strong></a> that extends iterators with the <a class="reference internal" href="../reference/expressions.html#generator.send" title="generator.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a>, <a class="reference internal" href="../reference/expressions.html#generator.throw" title="generator.throw"><code class="xref py py-meth docutils literal"><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"><span class="pre">close()</span></code></a> methods. </span><span class="yiyi-st" id="yiyi-104">另见<a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a>的定义。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-105"><span class="versionmodified">版本3.5中的新功能。</span></span></p></div></dd></dl><dl class="class"><dt id="collections.abc.Sequence"><span class="yiyi-st" id="yiyi-106"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Sequence</code></span></dt><dt id="collections.abc.MutableSequence"><span class="yiyi-st" id="yiyi-107"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">MutableSequence</code></span></dt><dt id="collections.abc.ByteString"><span class="yiyi-st" id="yiyi-108"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">ByteString</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-109">只读和可变<a class="reference internal" href="../glossary.html#term-sequence"><span class="xref std std-term">sequences</span></a>的ABCs。</span></p><p><span class="yiyi-st" id="yiyi-110">实现注意:一些mixin方法,如<a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal"><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"><span class="pre">__reversed__()</span></code></a>和<code class="xref py py-meth docutils literal"><span class="pre">index()</span></code> <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-111">因此,如果<a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>以常量访问速度实现,则mixin方法将具有线性性能;然而,如果底层方法是线性的(因为它将与一个链表),mixins将具有二次性能,可能需要重写。</span></p><div class="versionchanged"><p><span class="yiyi-st" id="yiyi-112"><span class="versionmodified">在版本3.5中更改:</span> index()方法添加了对<em>停止</em>和<em>开始</em>参数的支持。</span></p></div></dd></dl><dl class="class"><dt id="collections.abc.Set"><span class="yiyi-st" id="yiyi-113"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Set</code></span></dt><dt id="collections.abc.MutableSet"><span class="yiyi-st" id="yiyi-114"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">MutableSet</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-115">ABCs用于只读和可变集。</span></p></dd></dl><dl class="class"><dt id="collections.abc.Mapping"><span class="yiyi-st" id="yiyi-116"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Mapping</code></span></dt><dt id="collections.abc.MutableMapping"><span class="yiyi-st" id="yiyi-117"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">MutableMapping</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-118">只读和可变<a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mappings</span></a>的ABCs。</span></p></dd></dl><dl class="class"><dt id="collections.abc.MappingView"><span class="yiyi-st" id="yiyi-119"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">MappingView</code></span></dt><dt id="collections.abc.ItemsView"><span class="yiyi-st" id="yiyi-120"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">ItemsView</code></span></dt><dt id="collections.abc.KeysView"><span class="yiyi-st" id="yiyi-121"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">KeysView</code></span></dt><dt id="collections.abc.ValuesView"><span class="yiyi-st" id="yiyi-122"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">ValuesView</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-123">用于映射的ABCs,项目,键和值<a class="reference internal" href="../glossary.html#term-dictionary-view"><span class="xref std std-term">views</span></a>。</span></p></dd></dl><dl class="class"><dt id="collections.abc.Awaitable"><span class="yiyi-st" id="yiyi-124"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Awaitable</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-125">用于<a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a>对象的ABC,可用于<a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal"><span class="pre">await</span></code></a>表达式中。</span><span class="yiyi-st" id="yiyi-126">自定义实现必须提供<a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal"><span class="pre">__ await __()</span></code></a>方法。</span></p><p><span class="yiyi-st" id="yiyi-127"><a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">协程</span></a>对象和<a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal"><span class="pre">协程</span></code></a> ABC的实例都是此ABC的实例。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-128">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-129">在CPython中,基于生成器的协程(用<a class="reference internal" href="types.html#types.coroutine" title="types.coroutine"><code class="xref py py-func docutils literal"><span class="pre">types.coroutine()</span></code></a>或<a class="reference internal" href="asyncio-task.html#asyncio.coroutine" title="asyncio.coroutine"><code class="xref py py-func docutils literal"><span class="pre">asyncio.coroutine()</span></code></a>装饰的生成器)是<em>awaitables</em>他们没有<a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal"><span class="pre">__ await __()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-130">使用<code class="docutils literal"><span class="pre">isinstance(gencoro,</span> <span class="pre">Awaitable)</span></code>,将返回<code class="docutils literal"><span class="pre">False</span></code>。</span><span class="yiyi-st" id="yiyi-131">使用<a class="reference internal" href="inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal"><span class="pre">inspect.isawaitable()</span></code></a>检测它们。</span></p></div><div class="versionadded"><p><span class="yiyi-st" id="yiyi-132"><span class="versionmodified">版本3.5中的新功能。</span></span></p></div></dd></dl><dl class="class"><dt id="collections.abc.Coroutine"><span class="yiyi-st" id="yiyi-133"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">Coroutine</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-134">协程兼容类ABC。</span><span class="yiyi-st" id="yiyi-135">这些实现在<a class="reference internal" href="../reference/datamodel.html#coroutine-objects"><span>协程对象</span></a>中定义的以下方法:<a class="reference internal" href="../reference/datamodel.html#coroutine.send" title="coroutine.send"><code class="xref py py-meth docutils literal"><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"><span class="pre">throw()</span></code></a>,和<a class="reference internal" href="../reference/datamodel.html#coroutine.close" title="coroutine.close"><code class="xref py py-meth docutils literal"><span class="pre">close() </span></code></a>。</span><span class="yiyi-st" id="yiyi-136">自定义实现还必须实现<a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal"><span class="pre">__ await __()</span></code></a>。</span><span class="yiyi-st" id="yiyi-137">所有<a class="reference internal" href="#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal"><span class="pre">协程</span></code></a>实例也是<a class="reference internal" href="#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal"><span class="pre">Awaitable</span></code></a>的实例。</span><span class="yiyi-st" id="yiyi-138">另见<a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">协程</span></a>的定义。</span></p><div class="admonition note"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-139">注意</span></p><p class="last"><span class="yiyi-st" id="yiyi-140">在CPython中,基于生成器的协程(<a class="reference internal" href="types.html#types.coroutine" title="types.coroutine"><code class="xref py py-func docutils literal"><span class="pre">types.coroutine()</span></code></a>或<a class="reference internal" href="asyncio-task.html#asyncio.coroutine" title="asyncio.coroutine"><code class="xref py py-func docutils literal"><span class="pre">asyncio.coroutine()</span></code></a>装饰的生成器是<em>awaitables</em>虽然他们没有<a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal"><span class="pre">__await__()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-141">使用<code class="docutils literal"><span class="pre">isinstance(gencoro,</span> <span class="pre">协程)</span></code>,将返回<code class="docutils literal"><span class="pre">False</span></code>。</span><span class="yiyi-st" id="yiyi-142">使用<a class="reference internal" href="inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal"><span class="pre">inspect.isawaitable()</span></code></a>检测它们。</span></p></div><div class="versionadded"><p><span class="yiyi-st" id="yiyi-143"><span class="versionmodified">版本3.5中的新功能。</span></span></p></div></dd></dl><dl class="class"><dt id="collections.abc.AsyncIterable"><span class="yiyi-st" id="yiyi-144"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">AsyncIterable</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-145">ABC提供<code class="docutils literal"><span class="pre">__aiter__</span></code>方法的类。</span><span class="yiyi-st" id="yiyi-146">另请参见<a class="reference internal" href="../glossary.html#term-asynchronous-iterable"><span class="xref std std-term">asynchronous iterable</span></a>的定义。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-147"><span class="versionmodified">版本3.5中的新功能。</span></span></p></div></dd></dl><dl class="class"><dt id="collections.abc.AsyncIterator"><span class="yiyi-st" id="yiyi-148"><em class="property">class </em><code class="descclassname">collections.abc.</code><code class="descname">AsyncIterator</code></span></dt><dd><p><span class="yiyi-st" id="yiyi-149">提供<code class="docutils literal"><span class="pre">__aiter__</span></code>和<code class="docutils literal"><span class="pre">__anext__</span></code>方法的类的ABC。</span><span class="yiyi-st" id="yiyi-150">另请参见<a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">asynchronous iterator</span></a>的定义。</span></p><div class="versionadded"><p><span class="yiyi-st" id="yiyi-151"><span class="versionmodified">版本3.5中的新功能。</span></span></p></div></dd></dl><p><span class="yiyi-st" id="yiyi-152">这些ABC允许我们询问类或实例,如果它们提供特定的功能,例如:</span></p><pre><code class="language-python"><span></span><span class="n">size</span> <span class="o">=</span> <span class="kc">None</span>
|
||
<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>
|
||
<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>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-153">几个ABCs也可用作mixin,使得更容易开发支持容器API的类。</span><span class="yiyi-st" id="yiyi-154">For example, to write a class supporting the full <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> API, it is only necessary to supply the three underlying abstract methods: <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></code></a>, and <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><code class="xref py py-meth docutils literal"><span class="pre">__len__()</span></code></a>. </span><span class="yiyi-st" id="yiyi-155">ABC提供诸如<a class="reference internal" href="../reference/datamodel.html#object.__and__" title="object.__and__"><code class="xref py py-meth docutils literal"><span class="pre">__and__()</span></code></a>和<code class="xref py py-meth docutils literal"><span class="pre">isdisjoint()</span></code>之类的剩余方法:</span></p><pre><code class="language-python"><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>
|
||
<span class="sd">''' Alternate set implementation favoring space over speed</span>
|
||
<span class="sd"> and not requiring the set elements to be hashable. '''</span>
|
||
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span>
|
||
<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>
|
||
<span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
|
||
<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>
|
||
<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>
|
||
|
||
<span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<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>
|
||
|
||
<span class="k">def</span> <span class="nf">__contains__</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="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>
|
||
|
||
<span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<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>
|
||
|
||
<span class="n">s1</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s1">'abcdef'</span><span class="p">)</span>
|
||
<span class="n">s2</span> <span class="o">=</span> <span class="n">ListBasedSet</span><span class="p">(</span><span class="s1">'defghi'</span><span class="p">)</span>
|
||
<span class="n">overlap</span> <span class="o">=</span> <span class="n">s1</span> <span class="o">&</span> <span class="n">s2</span> <span class="c1"># The __and__() method is supported automatically</span>
|
||
</code></pre><p><span class="yiyi-st" id="yiyi-156">使用<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a>和<a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal"><span class="pre">MutableSet</span></code></a>作为混合的注意事项:</span></p><ol class="arabic simple"><li><span class="yiyi-st" id="yiyi-157">因为一些集合操作创建新集合,所以默认的mixin方法需要一种从迭代中创建新实例的方法。</span><span class="yiyi-st" id="yiyi-158">类构造函数假定具有<code class="docutils literal"><span class="pre">ClassName(iterable)</span></code>形式的声明。</span><span class="yiyi-st" id="yiyi-159">该假设被分解为调用<code class="docutils literal"><span class="pre">cls(iterable)</span></code>的一个名为<code class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></code>的内部类方法,以产生一个新集合。</span><span class="yiyi-st" id="yiyi-160">如果在具有不同构造函数声明的类中使用<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> mixin,则需要使用可以从可迭代参数构造新实例的类方法覆盖<code class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></code></span></li><li><span class="yiyi-st" id="yiyi-161">要覆盖比较(假定为速度,因为语义是固定的),重新定义<a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><code class="xref py py-meth docutils literal"><span class="pre">__le__()</span></code></a>和<a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><code class="xref py py-meth docutils literal"><span class="pre">__ge__()</span></code></a>,则其他操作将自动跟随。</span></li><li><span class="yiyi-st" id="yiyi-162"><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> mixin提供了一个<code class="xref py py-meth docutils literal"><span class="pre">_hash()</span></code>方法来计算集合的哈希值;然而,<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a>未定义,因为并非所有集都是可哈希的或不可变的。</span><span class="yiyi-st" id="yiyi-163">要使用mixins添加集合散列,请继承<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><code class="xref py py-meth docutils literal"><span class="pre">Set()</span></code></a>和<a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><code class="xref py py-meth docutils literal"><span class="pre">Hashable()</span></code></a>,然后定义<code class="docutils literal"><span class="pre">__ hash __</span> <span class="pre">= <span class="pre">Set._hash</span></span></code>。</span></li></ol><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-164">也可以看看</span></p><ul class="last simple"><li><span class="yiyi-st" id="yiyi-165"><a class="reference external" href="https://code.activestate.com/recipes/576694/">OrderedSet recipe</a>用于构建在<a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><code class="xref py py-class docutils literal"><span class="pre">MutableSet</span></code></a>上的示例。</span></li><li><span class="yiyi-st" id="yiyi-166">有关ABCs的更多信息,请参阅<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"><span class="pre">abc</span></code></a>模块和<span class="target" id="index-1"></span> <a class="pep reference external" href="https://www.python.org/dev/peps/pep-3119"><strong>PEP 3119</strong></a>。</span></li></ul></div></div></div></div> |