Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 1 | :mod:`collections.abc` --- Abstract Base Classes for Containers |
| 2 | =============================================================== |
| 3 | |
| 4 | .. module:: collections.abc |
| 5 | :synopsis: Abstract base classes for containers |
| 6 | .. moduleauthor:: Raymond Hettinger <python at rcn.com> |
| 7 | .. sectionauthor:: Raymond Hettinger <python at rcn.com> |
| 8 | |
Éric Araujo | b389eec | 2011-08-16 19:10:24 +0200 | [diff] [blame] | 9 | .. versionadded:: 3.3 |
| 10 | Formerly, this module was part of the :mod:`collections` module. |
| 11 | |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 12 | .. testsetup:: * |
| 13 | |
| 14 | from collections import * |
| 15 | import itertools |
| 16 | __name__ = '<doctest>' |
| 17 | |
| 18 | **Source code:** :source:`Lib/collections/abc.py` |
| 19 | |
| 20 | -------------- |
| 21 | |
| 22 | This module provides :term:`abstract base classes <abstract base class>` that |
| 23 | can be used to test whether a class provides a particular interface; for |
| 24 | example, whether it is hashable or whether it is a mapping. |
| 25 | |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 26 | |
Éric Araujo | f90112e | 2011-06-03 19:18:41 +0200 | [diff] [blame] | 27 | .. _collections-abstract-base-classes: |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 28 | |
| 29 | Collections Abstract Base Classes |
| 30 | --------------------------------- |
| 31 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 32 | The collections module offers the following :term:`ABCs <abstract base class>`: |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 33 | |
| 34 | ========================= ===================== ====================== ==================================================== |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 35 | ABC Inherits from Abstract Methods Mixin Methods |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 36 | ========================= ===================== ====================== ==================================================== |
| 37 | :class:`Container` ``__contains__`` |
| 38 | :class:`Hashable` ``__hash__`` |
| 39 | :class:`Iterable` ``__iter__`` |
| 40 | :class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__`` |
| 41 | :class:`Sized` ``__len__`` |
| 42 | :class:`Callable` ``__call__`` |
| 43 | |
| 44 | :class:`Sequence` :class:`Sized`, ``__getitem__`` ``__contains__``, ``__iter__``, ``__reversed__``, |
| 45 | :class:`Iterable`, ``index``, and ``count`` |
| 46 | :class:`Container` |
| 47 | |
Éric Araujo | 459b452 | 2011-06-04 21:16:42 +0200 | [diff] [blame] | 48 | :class:`MutableSequence` :class:`Sequence` ``__setitem__``, Inherited :class:`Sequence` methods and |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 49 | ``__delitem__``, ``append``, ``reverse``, ``extend``, ``pop``, |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 50 | ``insert`` ``remove``, ``clear``, and ``__iadd__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 51 | |
| 52 | :class:`Set` :class:`Sized`, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``, |
| 53 | :class:`Iterable`, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``, |
| 54 | :class:`Container` ``__sub__``, ``__xor__``, and ``isdisjoint`` |
| 55 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 56 | :class:`MutableSet` :class:`Set` ``add``, Inherited :class:`Set` methods and |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 57 | ``discard`` ``clear``, ``pop``, ``remove``, ``__ior__``, |
| 58 | ``__iand__``, ``__ixor__``, and ``__isub__`` |
| 59 | |
| 60 | :class:`Mapping` :class:`Sized`, ``__getitem__`` ``__contains__``, ``keys``, ``items``, ``values``, |
| 61 | :class:`Iterable`, ``get``, ``__eq__``, and ``__ne__`` |
| 62 | :class:`Container` |
| 63 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 64 | :class:`MutableMapping` :class:`Mapping` ``__setitem__``, Inherited :class:`Mapping` methods and |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 65 | ``__delitem__`` ``pop``, ``popitem``, ``clear``, ``update``, |
| 66 | and ``setdefault`` |
| 67 | |
| 68 | |
| 69 | :class:`MappingView` :class:`Sized` ``__len__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 70 | :class:`ItemsView` :class:`MappingView`, ``__contains__``, |
| 71 | :class:`Set` ``__iter__`` |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 72 | :class:`KeysView` :class:`MappingView`, ``__contains__``, |
| 73 | :class:`Set` ``__iter__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 74 | :class:`ValuesView` :class:`MappingView` ``__contains__``, ``__iter__`` |
| 75 | ========================= ===================== ====================== ==================================================== |
| 76 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 77 | |
| 78 | .. class:: Container |
| 79 | Hashable |
| 80 | Sized |
| 81 | Callable |
| 82 | |
| 83 | ABCs for classes that provide respectively the methods :meth:`__contains__`, |
| 84 | :meth:`__hash__`, :meth:`__len__`, and :meth:`__call__`. |
| 85 | |
| 86 | .. class:: Iterable |
| 87 | |
| 88 | ABC for classes that provide the :meth:`__iter__` method. |
| 89 | See also the definition of :term:`iterable`. |
| 90 | |
| 91 | .. class:: Iterator |
| 92 | |
Andrew Svetlov | e2303f8 | 2012-12-18 15:53:15 +0200 | [diff] [blame] | 93 | ABC for classes that provide the :meth:`__iter__` and :meth:`__next__` methods. |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 94 | See also the definition of :term:`iterator`. |
| 95 | |
| 96 | .. class:: Sequence |
| 97 | MutableSequence |
| 98 | |
| 99 | ABCs for read-only and mutable :term:`sequences <sequence>`. |
| 100 | |
| 101 | .. class:: Set |
| 102 | MutableSet |
| 103 | |
| 104 | ABCs for read-only and mutable sets. |
| 105 | |
| 106 | .. class:: Mapping |
| 107 | MutableMapping |
| 108 | |
| 109 | ABCs for read-only and mutable :term:`mappings <mapping>`. |
| 110 | |
| 111 | .. class:: MappingView |
| 112 | ItemsView |
| 113 | KeysView |
| 114 | ValuesView |
| 115 | |
| 116 | ABCs for mapping, items, keys, and values :term:`views <view>`. |
| 117 | |
| 118 | |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 119 | These ABCs allow us to ask classes or instances if they provide |
| 120 | particular functionality, for example:: |
| 121 | |
| 122 | size = None |
| 123 | if isinstance(myvar, collections.Sized): |
| 124 | size = len(myvar) |
| 125 | |
| 126 | Several of the ABCs are also useful as mixins that make it easier to develop |
| 127 | classes supporting container APIs. For example, to write a class supporting |
| 128 | the full :class:`Set` API, it only necessary to supply the three underlying |
| 129 | abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`. |
| 130 | The ABC supplies the remaining methods such as :meth:`__and__` and |
| 131 | :meth:`isdisjoint` :: |
| 132 | |
| 133 | class ListBasedSet(collections.Set): |
| 134 | ''' Alternate set implementation favoring space over speed |
| 135 | and not requiring the set elements to be hashable. ''' |
| 136 | def __init__(self, iterable): |
| 137 | self.elements = lst = [] |
| 138 | for value in iterable: |
| 139 | if value not in lst: |
| 140 | lst.append(value) |
| 141 | def __iter__(self): |
| 142 | return iter(self.elements) |
| 143 | def __contains__(self, value): |
| 144 | return value in self.elements |
| 145 | def __len__(self): |
| 146 | return len(self.elements) |
| 147 | |
| 148 | s1 = ListBasedSet('abcdef') |
| 149 | s2 = ListBasedSet('defghi') |
| 150 | overlap = s1 & s2 # The __and__() method is supported automatically |
| 151 | |
| 152 | Notes on using :class:`Set` and :class:`MutableSet` as a mixin: |
| 153 | |
| 154 | (1) |
| 155 | Since some set operations create new sets, the default mixin methods need |
| 156 | a way to create new instances from an iterable. The class constructor is |
| 157 | assumed to have a signature in the form ``ClassName(iterable)``. |
| 158 | That assumption is factored-out to an internal classmethod called |
| 159 | :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set. |
| 160 | If the :class:`Set` mixin is being used in a class with a different |
Antoine Pitrou | 3692035 | 2011-03-22 18:33:33 +0100 | [diff] [blame] | 161 | constructor signature, you will need to override :meth:`_from_iterable` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 162 | with a classmethod that can construct new instances from |
| 163 | an iterable argument. |
| 164 | |
| 165 | (2) |
| 166 | To override the comparisons (presumably for speed, as the |
| 167 | semantics are fixed), redefine :meth:`__le__` and |
| 168 | then the other operations will automatically follow suit. |
| 169 | |
| 170 | (3) |
| 171 | The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value |
| 172 | for the set; however, :meth:`__hash__` is not defined because not all sets |
| 173 | are hashable or immutable. To add set hashabilty using mixins, |
| 174 | inherit from both :meth:`Set` and :meth:`Hashable`, then define |
| 175 | ``__hash__ = Set._hash``. |
| 176 | |
| 177 | .. seealso:: |
| 178 | |
Éric Araujo | 459b452 | 2011-06-04 21:16:42 +0200 | [diff] [blame] | 179 | * `OrderedSet recipe <http://code.activestate.com/recipes/576694/>`_ for an |
| 180 | example built on :class:`MutableSet`. |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 181 | |
Éric Araujo | 459b452 | 2011-06-04 21:16:42 +0200 | [diff] [blame] | 182 | * For more about ABCs, see the :mod:`abc` module and :pep:`3119`. |