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 | |
Christian Heimes | f1dc3ee | 2013-10-13 02:04:20 +0200 | [diff] [blame] | 18 | **Source code:** :source:`Lib/_collections_abc.py` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 34 | .. tabularcolumns:: |l|L|L|L| |
| 35 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 36 | ========================== ====================== ======================= ==================================================== |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 37 | ABC Inherits from Abstract Methods Mixin Methods |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 38 | ========================== ====================== ======================= ==================================================== |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 39 | :class:`Container` ``__contains__`` |
| 40 | :class:`Hashable` ``__hash__`` |
| 41 | :class:`Iterable` ``__iter__`` |
| 42 | :class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__`` |
Raymond Hettinger | bd60e8d | 2015-05-09 01:07:23 -0400 | [diff] [blame] | 43 | :class:`Generator` :class:`Iterator` ``send``, ``throw`` ``close``, ``__iter__``, ``__next__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 44 | :class:`Sized` ``__len__`` |
| 45 | :class:`Callable` ``__call__`` |
| 46 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 47 | :class:`Sequence` :class:`Sized`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``, |
| 48 | :class:`Iterable`, ``__len__`` ``index``, and ``count`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 49 | :class:`Container` |
| 50 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 51 | :class:`MutableSequence` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods and |
| 52 | ``__setitem__``, ``append``, ``reverse``, ``extend``, ``pop``, |
| 53 | ``__delitem__``, ``remove``, and ``__iadd__`` |
| 54 | ``__len__``, |
| 55 | ``insert`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 56 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 57 | :class:`Set` :class:`Sized`, ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``, |
| 58 | :class:`Iterable`, ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``, |
| 59 | :class:`Container` ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 60 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 61 | :class:`MutableSet` :class:`Set` ``__contains__``, Inherited :class:`Set` methods and |
| 62 | ``__iter__``, ``clear``, ``pop``, ``remove``, ``__ior__``, |
| 63 | ``__len__``, ``__iand__``, ``__ixor__``, and ``__isub__`` |
| 64 | ``add``, |
| 65 | ``discard`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 67 | :class:`Mapping` :class:`Sized`, ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``, |
| 68 | :class:`Iterable`, ``__iter__``, ``get``, ``__eq__``, and ``__ne__`` |
| 69 | :class:`Container` ``__len__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 70 | |
Raymond Hettinger | 3ddba16 | 2013-03-23 09:07:36 -0700 | [diff] [blame] | 71 | :class:`MutableMapping` :class:`Mapping` ``__getitem__``, Inherited :class:`Mapping` methods and |
| 72 | ``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``, |
| 73 | ``__delitem__``, and ``setdefault`` |
| 74 | ``__iter__``, |
| 75 | ``__len__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | :class:`MappingView` :class:`Sized` ``__len__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 79 | :class:`ItemsView` :class:`MappingView`, ``__contains__``, |
| 80 | :class:`Set` ``__iter__`` |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 81 | :class:`KeysView` :class:`MappingView`, ``__contains__``, |
| 82 | :class:`Set` ``__iter__`` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 83 | :class:`ValuesView` :class:`MappingView` ``__contains__``, ``__iter__`` |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 84 | :class:`Awaitable` ``__await__`` |
Yury Selivanov | 56fc614 | 2015-05-29 09:01:29 -0400 | [diff] [blame] | 85 | :class:`Coroutine` :class:`Awaitable` ``send``, ``throw`` ``close`` |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 86 | :class:`AsyncIterable` ``__aiter__`` |
| 87 | :class:`AsyncIterator` :class:`AsyncIterable` ``__anext__`` ``__aiter__`` |
| 88 | ========================== ====================== ======================= ==================================================== |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 89 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 90 | |
| 91 | .. class:: Container |
| 92 | Hashable |
| 93 | Sized |
| 94 | Callable |
| 95 | |
| 96 | ABCs for classes that provide respectively the methods :meth:`__contains__`, |
| 97 | :meth:`__hash__`, :meth:`__len__`, and :meth:`__call__`. |
| 98 | |
| 99 | .. class:: Iterable |
| 100 | |
| 101 | ABC for classes that provide the :meth:`__iter__` method. |
| 102 | See also the definition of :term:`iterable`. |
| 103 | |
| 104 | .. class:: Iterator |
| 105 | |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 106 | ABC for classes that provide the :meth:`~iterator.__iter__` and |
| 107 | :meth:`~iterator.__next__` methods. See also the definition of |
| 108 | :term:`iterator`. |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 109 | |
Raymond Hettinger | bd60e8d | 2015-05-09 01:07:23 -0400 | [diff] [blame] | 110 | .. class:: Generator |
| 111 | |
| 112 | ABC for generator classes that implement the protocol defined in |
| 113 | :pep:`342` that extends iterators with the :meth:`~generator.send`, |
| 114 | :meth:`~generator.throw` and :meth:`~generator.close` methods. |
| 115 | See also the definition of :term:`generator`. |
| 116 | |
| 117 | .. versionadded:: 3.5 |
| 118 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 119 | .. class:: Sequence |
| 120 | MutableSequence |
| 121 | |
| 122 | ABCs for read-only and mutable :term:`sequences <sequence>`. |
| 123 | |
Raymond Hettinger | ec219ba | 2015-05-22 19:29:22 -0700 | [diff] [blame] | 124 | Implementation note: Some of the mixin methods, such as |
| 125 | :meth:`__iter__`, :meth:`__reversed__` and :meth:`index`, make |
| 126 | repeated calls to the underlying :meth:`__getitem__` method. |
| 127 | Consequently, if :meth:`__getitem__` is implemented with constant |
| 128 | access speed, the mixin methods will have linear performance; |
| 129 | however, if the underlying method is linear (as it would be with a |
| 130 | linked list), the mixins will have quadratic performance and will |
| 131 | likely need to be overridden. |
| 132 | |
| 133 | .. versionchanged:: 3.5 |
| 134 | The index() method added support for *stop* and *start* |
| 135 | arguments. |
| 136 | |
| 137 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 138 | .. class:: Set |
| 139 | MutableSet |
| 140 | |
| 141 | ABCs for read-only and mutable sets. |
| 142 | |
| 143 | .. class:: Mapping |
| 144 | MutableMapping |
| 145 | |
| 146 | ABCs for read-only and mutable :term:`mappings <mapping>`. |
| 147 | |
| 148 | .. class:: MappingView |
| 149 | ItemsView |
| 150 | KeysView |
| 151 | ValuesView |
| 152 | |
| 153 | ABCs for mapping, items, keys, and values :term:`views <view>`. |
| 154 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 155 | .. class:: Awaitable |
| 156 | |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 157 | ABC for :term:`awaitable` objects, which can be used in :keyword:`await` |
| 158 | expressions. Custom implementations must provide the :meth:`__await__` |
| 159 | method. |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 160 | |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 161 | :term:`Coroutine` objects and instances of the |
| 162 | :class:`~collections.abc.Coroutine` ABC are all instances of this ABC. |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 163 | |
Yury Selivanov | cc1d028 | 2015-07-01 12:49:00 -0400 | [diff] [blame] | 164 | .. note:: |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 165 | In CPython, generator-based coroutines (generators decorated with |
| 166 | :func:`types.coroutine` or :func:`asyncio.coroutine`) are |
| 167 | *awaitables*, even though they do not have an :meth:`__await__` method. |
| 168 | Using ``isinstance(gencoro, Awaitable)`` for them will return ``False``. |
| 169 | Use :func:`inspect.isawaitable` to detect them. |
Yury Selivanov | cc1d028 | 2015-07-01 12:49:00 -0400 | [diff] [blame] | 170 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 171 | .. versionadded:: 3.5 |
| 172 | |
| 173 | .. class:: Coroutine |
| 174 | |
Yury Selivanov | 66f8828 | 2015-06-24 11:04:15 -0400 | [diff] [blame] | 175 | ABC for coroutine compatible classes. These implement the |
| 176 | following methods, defined in :ref:`coroutine-objects`: |
| 177 | :meth:`~coroutine.send`, :meth:`~coroutine.throw`, and |
| 178 | :meth:`~coroutine.close`. Custom implementations must also implement |
| 179 | :meth:`__await__`. All :class:`Coroutine` instances are also instances of |
Yury Selivanov | 56fc614 | 2015-05-29 09:01:29 -0400 | [diff] [blame] | 180 | :class:`Awaitable`. See also the definition of :term:`coroutine`. |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 181 | |
Yury Selivanov | cc1d028 | 2015-07-01 12:49:00 -0400 | [diff] [blame] | 182 | .. note:: |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 183 | In CPython, generator-based coroutines (generators decorated with |
| 184 | :func:`types.coroutine` or :func:`asyncio.coroutine`) are |
| 185 | *awaitables*, even though they do not have an :meth:`__await__` method. |
| 186 | Using ``isinstance(gencoro, Coroutine)`` for them will return ``False``. |
| 187 | Use :func:`inspect.isawaitable` to detect them. |
Yury Selivanov | cc1d028 | 2015-07-01 12:49:00 -0400 | [diff] [blame] | 188 | |
Yury Selivanov | f3e40fa | 2015-05-21 11:50:30 -0400 | [diff] [blame] | 189 | .. versionadded:: 3.5 |
| 190 | |
| 191 | .. class:: AsyncIterable |
| 192 | |
| 193 | ABC for classes that provide ``__aiter__`` method. See also the |
| 194 | definition of :term:`asynchronous iterable`. |
| 195 | |
| 196 | .. versionadded:: 3.5 |
| 197 | |
| 198 | .. class:: AsyncIterator |
| 199 | |
| 200 | ABC for classes that provide ``__aiter__`` and ``__anext__`` |
| 201 | methods. See also the definition of :term:`asynchronous iterator`. |
| 202 | |
| 203 | .. versionadded:: 3.5 |
| 204 | |
Ezio Melotti | c831a91 | 2011-03-28 19:27:09 +0300 | [diff] [blame] | 205 | |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 206 | These ABCs allow us to ask classes or instances if they provide |
| 207 | particular functionality, for example:: |
| 208 | |
| 209 | size = None |
Georg Brandl | 5f4b4ac | 2013-04-14 10:50:05 +0200 | [diff] [blame] | 210 | if isinstance(myvar, collections.abc.Sized): |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 211 | size = len(myvar) |
| 212 | |
| 213 | Several of the ABCs are also useful as mixins that make it easier to develop |
| 214 | classes supporting container APIs. For example, to write a class supporting |
Georg Brandl | 8ed75cd | 2014-10-31 10:25:48 +0100 | [diff] [blame] | 215 | the full :class:`Set` API, it is only necessary to supply the three underlying |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 216 | abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`. |
| 217 | The ABC supplies the remaining methods such as :meth:`__and__` and |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 218 | :meth:`isdisjoint`:: |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 219 | |
Georg Brandl | 5f4b4ac | 2013-04-14 10:50:05 +0200 | [diff] [blame] | 220 | class ListBasedSet(collections.abc.Set): |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 221 | ''' Alternate set implementation favoring space over speed |
| 222 | and not requiring the set elements to be hashable. ''' |
| 223 | def __init__(self, iterable): |
| 224 | self.elements = lst = [] |
| 225 | for value in iterable: |
| 226 | if value not in lst: |
| 227 | lst.append(value) |
| 228 | def __iter__(self): |
| 229 | return iter(self.elements) |
| 230 | def __contains__(self, value): |
| 231 | return value in self.elements |
| 232 | def __len__(self): |
| 233 | return len(self.elements) |
| 234 | |
| 235 | s1 = ListBasedSet('abcdef') |
| 236 | s2 = ListBasedSet('defghi') |
| 237 | overlap = s1 & s2 # The __and__() method is supported automatically |
| 238 | |
| 239 | Notes on using :class:`Set` and :class:`MutableSet` as a mixin: |
| 240 | |
| 241 | (1) |
| 242 | Since some set operations create new sets, the default mixin methods need |
| 243 | a way to create new instances from an iterable. The class constructor is |
| 244 | assumed to have a signature in the form ``ClassName(iterable)``. |
| 245 | That assumption is factored-out to an internal classmethod called |
| 246 | :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set. |
| 247 | 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] | 248 | constructor signature, you will need to override :meth:`_from_iterable` |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 249 | with a classmethod that can construct new instances from |
| 250 | an iterable argument. |
| 251 | |
| 252 | (2) |
| 253 | To override the comparisons (presumably for speed, as the |
Raymond Hettinger | 11cda47 | 2014-07-03 00:31:30 +0100 | [diff] [blame] | 254 | semantics are fixed), redefine :meth:`__le__` and :meth:`__ge__`, |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 255 | then the other operations will automatically follow suit. |
| 256 | |
| 257 | (3) |
| 258 | The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value |
| 259 | for the set; however, :meth:`__hash__` is not defined because not all sets |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 260 | are hashable or immutable. To add set hashability using mixins, |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 261 | inherit from both :meth:`Set` and :meth:`Hashable`, then define |
| 262 | ``__hash__ = Set._hash``. |
| 263 | |
| 264 | .. seealso:: |
| 265 | |
Éric Araujo | 459b452 | 2011-06-04 21:16:42 +0200 | [diff] [blame] | 266 | * `OrderedSet recipe <http://code.activestate.com/recipes/576694/>`_ for an |
| 267 | example built on :class:`MutableSet`. |
Raymond Hettinger | 158c9c2 | 2011-02-22 00:41:50 +0000 | [diff] [blame] | 268 | |
Éric Araujo | 459b452 | 2011-06-04 21:16:42 +0200 | [diff] [blame] | 269 | * For more about ABCs, see the :mod:`abc` module and :pep:`3119`. |