Sophie

Sophie

distrib > Mageia > 3 > i586 > media > core-updates > by-pkgid > 50402eac2a16508b365658612a898528 > files > 641

python3-docs-3.3.0-4.3.mga3.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.4. collections.abc — Abstract Base Classes for Containers &mdash; Python v3.3.0 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.3.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.3.0 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.3.0 documentation" href="../index.html" />
    <link rel="up" title="8. Data Types" href="datatypes.html" />
    <link rel="next" title="8.5. heapq — Heap queue algorithm" href="heapq.html" />
    <link rel="prev" title="8.3. collections — Container datatypes" href="collections.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="heapq.html" title="8.5. heapq — Heap queue algorithm"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="collections.html" title="8.3. collections — Container datatypes"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" accesskey="U">8. Data Types</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-collections.abc">
<span id="collections-abc-abstract-base-classes-for-containers"></span><h1>8.4. <a class="reference internal" href="#module-collections.abc" title="collections.abc: Abstract base classes for containers"><tt class="xref py py-mod docutils literal"><span class="pre">collections.abc</span></tt></a> &#8212; Abstract Base Classes for Containers<a class="headerlink" href="#module-collections.abc" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">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"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module.</p>
<p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/3.3/Lib/collections/abc.py">Lib/collections/abc.py</a></p>
<hr class="docutils" />
<p>This module provides <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base classes</em></a> that
can be used to test whether a class provides a particular interface; for
example, whether it is hashable or whether it is a mapping.</p>
<div class="section" id="collections-abstract-base-classes">
<span id="id1"></span><h2>8.4.1. Collections Abstract Base Classes<a class="headerlink" href="#collections-abstract-base-classes" title="Permalink to this headline">¶</a></h2>
<p>The collections module offers the following <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">ABCs</em></a>:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="18%" />
<col width="18%" />
<col width="43%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">ABC</th>
<th class="head">Inherits from</th>
<th class="head">Abstract Methods</th>
<th class="head">Mixin Methods</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><tt class="xref py py-class docutils literal"><span class="pre">Hashable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__hash__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Iterator" title="collections.abc.Iterator"><tt class="xref py py-class docutils literal"><span class="pre">Iterator</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__next__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__len__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Callable" title="collections.abc.Callable"><tt class="xref py py-class docutils literal"><span class="pre">Callable</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__call__</span></tt></td>
<td>&nbsp;</td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__getitem__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">__iter__</span></tt>, <tt class="docutils literal"><span class="pre">__reversed__</span></tt>,
<tt class="docutils literal"><span class="pre">index</span></tt>, and <tt class="docutils literal"><span class="pre">count</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.MutableSequence" title="collections.abc.MutableSequence"><tt class="xref py py-class docutils literal"><span class="pre">MutableSequence</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__setitem__</span></tt>,
<tt class="docutils literal"><span class="pre">__delitem__</span></tt>,
<tt class="docutils literal"><span class="pre">insert</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.abc.Sequence" title="collections.abc.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">append</span></tt>, <tt class="docutils literal"><span class="pre">reverse</span></tt>, <tt class="docutils literal"><span class="pre">extend</span></tt>, <tt class="docutils literal"><span class="pre">pop</span></tt>,
<tt class="docutils literal"><span class="pre">remove</span></tt>, <tt class="docutils literal"><span class="pre">clear</span></tt>, and <tt class="docutils literal"><span class="pre">__iadd__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__le__</span></tt>, <tt class="docutils literal"><span class="pre">__lt__</span></tt>, <tt class="docutils literal"><span class="pre">__eq__</span></tt>, <tt class="docutils literal"><span class="pre">__ne__</span></tt>,
<tt class="docutils literal"><span class="pre">__gt__</span></tt>, <tt class="docutils literal"><span class="pre">__ge__</span></tt>, <tt class="docutils literal"><span class="pre">__and__</span></tt>, <tt class="docutils literal"><span class="pre">__or__</span></tt>,
<tt class="docutils literal"><span class="pre">__sub__</span></tt>, <tt class="docutils literal"><span class="pre">__xor__</span></tt>, and <tt class="docutils literal"><span class="pre">isdisjoint</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">add</span></tt>,
<tt class="docutils literal"><span class="pre">discard</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">clear</span></tt>, <tt class="docutils literal"><span class="pre">pop</span></tt>, <tt class="docutils literal"><span class="pre">remove</span></tt>, <tt class="docutils literal"><span class="pre">__ior__</span></tt>,
<tt class="docutils literal"><span class="pre">__iand__</span></tt>, <tt class="docutils literal"><span class="pre">__ixor__</span></tt>, and <tt class="docutils literal"><span class="pre">__isub__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a>,
<a class="reference internal" href="#collections.abc.Iterable" title="collections.abc.Iterable"><tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt></a>,
<a class="reference internal" href="#collections.abc.Container" title="collections.abc.Container"><tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__getitem__</span></tt></td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">keys</span></tt>, <tt class="docutils literal"><span class="pre">items</span></tt>, <tt class="docutils literal"><span class="pre">values</span></tt>,
<tt class="docutils literal"><span class="pre">get</span></tt>, <tt class="docutils literal"><span class="pre">__eq__</span></tt>, and <tt class="docutils literal"><span class="pre">__ne__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><tt class="xref py py-class docutils literal"><span class="pre">MutableMapping</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a></td>
<td><tt class="docutils literal"><span class="pre">__setitem__</span></tt>,
<tt class="docutils literal"><span class="pre">__delitem__</span></tt></td>
<td>Inherited <a class="reference internal" href="#collections.abc.Mapping" title="collections.abc.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a> methods and
<tt class="docutils literal"><span class="pre">pop</span></tt>, <tt class="docutils literal"><span class="pre">popitem</span></tt>, <tt class="docutils literal"><span class="pre">clear</span></tt>, <tt class="docutils literal"><span class="pre">update</span></tt>,
and <tt class="docutils literal"><span class="pre">setdefault</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.Sized" title="collections.abc.Sized"><tt class="xref py py-class docutils literal"><span class="pre">Sized</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__len__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.ItemsView" title="collections.abc.ItemsView"><tt class="xref py py-class docutils literal"><span class="pre">ItemsView</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a>,
<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>,
<tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.KeysView" title="collections.abc.KeysView"><tt class="xref py py-class docutils literal"><span class="pre">KeysView</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a>,
<a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>,
<tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
<tr><td><a class="reference internal" href="#collections.abc.ValuesView" title="collections.abc.ValuesView"><tt class="xref py py-class docutils literal"><span class="pre">ValuesView</span></tt></a></td>
<td><a class="reference internal" href="#collections.abc.MappingView" title="collections.abc.MappingView"><tt class="xref py py-class docutils literal"><span class="pre">MappingView</span></tt></a></td>
<td>&nbsp;</td>
<td><tt class="docutils literal"><span class="pre">__contains__</span></tt>, <tt class="docutils literal"><span class="pre">__iter__</span></tt></td>
</tr>
</tbody>
</table>
<dl class="class">
<dt id="collections.abc.Container">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Container</tt><a class="headerlink" href="#collections.abc.Container" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.Hashable">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Hashable</tt><a class="headerlink" href="#collections.abc.Hashable" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.Sized">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Sized</tt><a class="headerlink" href="#collections.abc.Sized" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.Callable">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Callable</tt><a class="headerlink" href="#collections.abc.Callable" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for classes that provide respectively the methods <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>,
<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a>, and <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><tt class="xref py py-meth docutils literal"><span class="pre">__call__()</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.Iterable">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Iterable</tt><a class="headerlink" href="#collections.abc.Iterable" title="Permalink to this definition">¶</a></dt>
<dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> method.
See also the definition of <a class="reference internal" href="../glossary.html#term-iterable"><em class="xref std std-term">iterable</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.Iterator">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Iterator</tt><a class="headerlink" href="#collections.abc.Iterator" title="Permalink to this definition">¶</a></dt>
<dd><p>ABC for classes that provide the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> and <a class="reference internal" href="functions.html#next" title="next"><tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt></a> methods.
See also the definition of <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.Sequence">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Sequence</tt><a class="headerlink" href="#collections.abc.Sequence" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.MutableSequence">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">MutableSequence</tt><a class="headerlink" href="#collections.abc.MutableSequence" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-sequence"><em class="xref std std-term">sequences</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.Set">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Set</tt><a class="headerlink" href="#collections.abc.Set" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.MutableSet">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">MutableSet</tt><a class="headerlink" href="#collections.abc.MutableSet" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable sets.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.Mapping">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">Mapping</tt><a class="headerlink" href="#collections.abc.Mapping" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.MutableMapping">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">MutableMapping</tt><a class="headerlink" href="#collections.abc.MutableMapping" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for read-only and mutable <a class="reference internal" href="../glossary.html#term-mapping"><em class="xref std std-term">mappings</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="collections.abc.MappingView">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">MappingView</tt><a class="headerlink" href="#collections.abc.MappingView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.ItemsView">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">ItemsView</tt><a class="headerlink" href="#collections.abc.ItemsView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.KeysView">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">KeysView</tt><a class="headerlink" href="#collections.abc.KeysView" title="Permalink to this definition">¶</a></dt>
<dt id="collections.abc.ValuesView">
<em class="property">class </em><tt class="descclassname">collections.abc.</tt><tt class="descname">ValuesView</tt><a class="headerlink" href="#collections.abc.ValuesView" title="Permalink to this definition">¶</a></dt>
<dd><p>ABCs for mapping, items, keys, and values <a class="reference internal" href="../glossary.html#term-view"><em class="xref std std-term">views</em></a>.</p>
</dd></dl>

<p>These ABCs allow us to ask classes or instances if they provide
particular functionality, for example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">size</span> <span class="o">=</span> <span class="k">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">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>
</pre></div>
</div>
<p>Several of the ABCs are also useful as mixins that make it easier to develop
classes supporting container APIs.  For example, to write a class supporting
the full <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> API, it only necessary to supply the three underlying
abstract methods: <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a>, and <a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a>.
The ABC supplies the remaining methods such as <a class="reference internal" href="../reference/datamodel.html#object.__and__" title="object.__and__"><tt class="xref py py-meth docutils literal"><span class="pre">__and__()</span></tt></a> and
<tt class="xref py py-meth docutils literal"><span class="pre">isdisjoint()</span></tt></p>
<div class="highlight-python3"><div class="highlight"><pre><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">Set</span><span class="p">):</span>
     <span class="sd">&#39;&#39;&#39; Alternate set implementation favoring space over speed</span>
<span class="sd">         and not requiring the set elements to be hashable. &#39;&#39;&#39;</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="s">&#39;abcdef&#39;</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="s">&#39;defghi&#39;</span><span class="p">)</span>
<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="c"># The __and__() method is supported automatically</span>
</pre></div>
</div>
<p>Notes on using <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> and <a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a> as a mixin:</p>
<ol class="arabic simple">
<li>Since some set operations create new sets, the default mixin methods need
a way to create new instances from an iterable. The class constructor is
assumed to have a signature in the form <tt class="docutils literal"><span class="pre">ClassName(iterable)</span></tt>.
That assumption is factored-out to an internal classmethod called
<tt class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></tt> which calls <tt class="docutils literal"><span class="pre">cls(iterable)</span></tt> to produce a new set.
If the <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> mixin is being used in a class with a different
constructor signature, you will need to override <tt class="xref py py-meth docutils literal"><span class="pre">_from_iterable()</span></tt>
with a classmethod that can construct new instances from
an iterable argument.</li>
<li>To override the comparisons (presumably for speed, as the
semantics are fixed), redefine <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><tt class="xref py py-meth docutils literal"><span class="pre">__le__()</span></tt></a> and
then the other operations will automatically follow suit.</li>
<li>The <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-class docutils literal"><span class="pre">Set</span></tt></a> mixin provides a <tt class="xref py py-meth docutils literal"><span class="pre">_hash()</span></tt> method to compute a hash value
for the set; however, <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> is not defined because not all sets
are hashable or immutable.  To add set hashabilty using mixins,
inherit from both <a class="reference internal" href="#collections.abc.Set" title="collections.abc.Set"><tt class="xref py py-meth docutils literal"><span class="pre">Set()</span></tt></a> and <a class="reference internal" href="#collections.abc.Hashable" title="collections.abc.Hashable"><tt class="xref py py-meth docutils literal"><span class="pre">Hashable()</span></tt></a>, then define
<tt class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">Set._hash</span></tt>.</li>
</ol>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<ul class="last simple">
<li><a class="reference external" href="http://code.activestate.com/recipes/576694/">OrderedSet recipe</a> for an
example built on <a class="reference internal" href="#collections.abc.MutableSet" title="collections.abc.MutableSet"><tt class="xref py py-class docutils literal"><span class="pre">MutableSet</span></tt></a>.</li>
<li>For more about ABCs, see the <a class="reference internal" href="abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><tt class="xref py py-mod docutils literal"><span class="pre">abc</span></tt></a> module and <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3119"><strong>PEP 3119</strong></a>.</li>
</ul>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">8.4. <tt class="docutils literal"><span class="pre">collections.abc</span></tt> &#8212; Abstract Base Classes for Containers</a><ul>
<li><a class="reference internal" href="#collections-abstract-base-classes">8.4.1. Collections Abstract Base Classes</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="collections.html"
                        title="previous chapter">8.3. <tt class="docutils literal docutils literal docutils literal"><span class="pre">collections</span></tt> &#8212; Container datatypes</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="heapq.html"
                        title="next chapter">8.5. <tt class="docutils literal docutils literal"><span class="pre">heapq</span></tt> &#8212; Heap queue algorithm</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/collections.abc.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="heapq.html" title="8.5. heapq — Heap queue algorithm"
             >next</a> |</li>
        <li class="right" >
          <a href="collections.html" title="8.3. collections — Container datatypes"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li><a href="../index.html">3.3.0 Documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" >8. Data Types</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2012, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 29, 2012.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>