blob: 4b21932e431247566171d5a363b606ee4f54423f [file] [log] [blame]
Raymond Hettinger158c9c22011-02-22 00:41:50 +00001: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 Araujob389eec2011-08-16 19:10:24 +02009.. versionadded:: 3.3
10 Formerly, this module was part of the :mod:`collections` module.
11
Raymond Hettinger158c9c22011-02-22 00:41:50 +000012.. testsetup:: *
13
14 from collections import *
15 import itertools
16 __name__ = '<doctest>'
17
18**Source code:** :source:`Lib/collections/abc.py`
19
20--------------
21
22This module provides :term:`abstract base classes <abstract base class>` that
23can be used to test whether a class provides a particular interface; for
24example, whether it is hashable or whether it is a mapping.
25
Raymond Hettinger158c9c22011-02-22 00:41:50 +000026
Éric Araujof90112e2011-06-03 19:18:41 +020027.. _collections-abstract-base-classes:
Raymond Hettinger158c9c22011-02-22 00:41:50 +000028
29Collections Abstract Base Classes
30---------------------------------
31
Ezio Melottic831a912011-03-28 19:27:09 +030032The collections module offers the following :term:`ABCs <abstract base class>`:
Raymond Hettinger158c9c22011-02-22 00:41:50 +000033
Georg Brandl44ea77b2013-03-28 13:28:44 +010034.. tabularcolumns:: |l|L|L|L|
35
Raymond Hettinger158c9c22011-02-22 00:41:50 +000036========================= ===================== ====================== ====================================================
Ezio Melottic831a912011-03-28 19:27:09 +030037ABC Inherits from Abstract Methods Mixin Methods
Raymond Hettinger158c9c22011-02-22 00:41:50 +000038========================= ===================== ====================== ====================================================
39:class:`Container` ``__contains__``
40:class:`Hashable` ``__hash__``
41:class:`Iterable` ``__iter__``
42:class:`Iterator` :class:`Iterable` ``__next__`` ``__iter__``
43:class:`Sized` ``__len__``
44:class:`Callable` ``__call__``
45
Raymond Hettinger3ddba162013-03-23 09:07:36 -070046:class:`Sequence` :class:`Sized`, ``__getitem__``, ``__contains__``, ``__iter__``, ``__reversed__``,
47 :class:`Iterable`, ``__len__`` ``index``, and ``count``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000048 :class:`Container`
49
Raymond Hettinger3ddba162013-03-23 09:07:36 -070050:class:`MutableSequence` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods and
51 ``__setitem__``, ``append``, ``reverse``, ``extend``, ``pop``,
52 ``__delitem__``, ``remove``, and ``__iadd__``
53 ``__len__``,
54 ``insert``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000055
Raymond Hettinger3ddba162013-03-23 09:07:36 -070056:class:`Set` :class:`Sized`, ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
57 :class:`Iterable`, ``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``,
58 :class:`Container` ``__len__`` ``__sub__``, ``__xor__``, and ``isdisjoint``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000059
Raymond Hettinger3ddba162013-03-23 09:07:36 -070060:class:`MutableSet` :class:`Set` ``__contains__``, Inherited :class:`Set` methods and
61 ``__iter__``, ``clear``, ``pop``, ``remove``, ``__ior__``,
62 ``__len__``, ``__iand__``, ``__ixor__``, and ``__isub__``
63 ``add``,
64 ``discard``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000065
Raymond Hettinger3ddba162013-03-23 09:07:36 -070066:class:`Mapping` :class:`Sized`, ``__getitem__``, ``__contains__``, ``keys``, ``items``, ``values``,
67 :class:`Iterable`, ``__iter__``, ``get``, ``__eq__``, and ``__ne__``
68 :class:`Container` ``__len__``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000069
Raymond Hettinger3ddba162013-03-23 09:07:36 -070070:class:`MutableMapping` :class:`Mapping` ``__getitem__``, Inherited :class:`Mapping` methods and
71 ``__setitem__``, ``pop``, ``popitem``, ``clear``, ``update``,
72 ``__delitem__``, and ``setdefault``
73 ``__iter__``,
74 ``__len__``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000075
76
77:class:`MappingView` :class:`Sized` ``__len__``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000078:class:`ItemsView` :class:`MappingView`, ``__contains__``,
79 :class:`Set` ``__iter__``
Ezio Melottic831a912011-03-28 19:27:09 +030080:class:`KeysView` :class:`MappingView`, ``__contains__``,
81 :class:`Set` ``__iter__``
Raymond Hettinger158c9c22011-02-22 00:41:50 +000082:class:`ValuesView` :class:`MappingView` ``__contains__``, ``__iter__``
83========================= ===================== ====================== ====================================================
84
Ezio Melottic831a912011-03-28 19:27:09 +030085
86.. class:: Container
87 Hashable
88 Sized
89 Callable
90
91 ABCs for classes that provide respectively the methods :meth:`__contains__`,
92 :meth:`__hash__`, :meth:`__len__`, and :meth:`__call__`.
93
94.. class:: Iterable
95
96 ABC for classes that provide the :meth:`__iter__` method.
97 See also the definition of :term:`iterable`.
98
99.. class:: Iterator
100
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300101 ABC for classes that provide the :meth:`~iterator.__iter__` and
102 :meth:`~iterator.__next__` methods. See also the definition of
103 :term:`iterator`.
Ezio Melottic831a912011-03-28 19:27:09 +0300104
105.. class:: Sequence
106 MutableSequence
107
108 ABCs for read-only and mutable :term:`sequences <sequence>`.
109
110.. class:: Set
111 MutableSet
112
113 ABCs for read-only and mutable sets.
114
115.. class:: Mapping
116 MutableMapping
117
118 ABCs for read-only and mutable :term:`mappings <mapping>`.
119
120.. class:: MappingView
121 ItemsView
122 KeysView
123 ValuesView
124
125 ABCs for mapping, items, keys, and values :term:`views <view>`.
126
127
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000128These ABCs allow us to ask classes or instances if they provide
129particular functionality, for example::
130
131 size = None
Georg Brandl5f4b4ac2013-04-14 10:50:05 +0200132 if isinstance(myvar, collections.abc.Sized):
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000133 size = len(myvar)
134
135Several of the ABCs are also useful as mixins that make it easier to develop
136classes supporting container APIs. For example, to write a class supporting
137the full :class:`Set` API, it only necessary to supply the three underlying
138abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`.
139The ABC supplies the remaining methods such as :meth:`__and__` and
Georg Brandl44ea77b2013-03-28 13:28:44 +0100140:meth:`isdisjoint`::
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000141
Georg Brandl5f4b4ac2013-04-14 10:50:05 +0200142 class ListBasedSet(collections.abc.Set):
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000143 ''' Alternate set implementation favoring space over speed
144 and not requiring the set elements to be hashable. '''
145 def __init__(self, iterable):
146 self.elements = lst = []
147 for value in iterable:
148 if value not in lst:
149 lst.append(value)
150 def __iter__(self):
151 return iter(self.elements)
152 def __contains__(self, value):
153 return value in self.elements
154 def __len__(self):
155 return len(self.elements)
156
157 s1 = ListBasedSet('abcdef')
158 s2 = ListBasedSet('defghi')
159 overlap = s1 & s2 # The __and__() method is supported automatically
160
161Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
162
163(1)
164 Since some set operations create new sets, the default mixin methods need
165 a way to create new instances from an iterable. The class constructor is
166 assumed to have a signature in the form ``ClassName(iterable)``.
167 That assumption is factored-out to an internal classmethod called
168 :meth:`_from_iterable` which calls ``cls(iterable)`` to produce a new set.
169 If the :class:`Set` mixin is being used in a class with a different
Antoine Pitrou36920352011-03-22 18:33:33 +0100170 constructor signature, you will need to override :meth:`_from_iterable`
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000171 with a classmethod that can construct new instances from
172 an iterable argument.
173
174(2)
175 To override the comparisons (presumably for speed, as the
176 semantics are fixed), redefine :meth:`__le__` and
177 then the other operations will automatically follow suit.
178
179(3)
180 The :class:`Set` mixin provides a :meth:`_hash` method to compute a hash value
181 for the set; however, :meth:`__hash__` is not defined because not all sets
182 are hashable or immutable. To add set hashabilty using mixins,
183 inherit from both :meth:`Set` and :meth:`Hashable`, then define
184 ``__hash__ = Set._hash``.
185
186.. seealso::
187
Éric Araujo459b4522011-06-04 21:16:42 +0200188 * `OrderedSet recipe <http://code.activestate.com/recipes/576694/>`_ for an
189 example built on :class:`MutableSet`.
Raymond Hettinger158c9c22011-02-22 00:41:50 +0000190
Éric Araujo459b4522011-06-04 21:16:42 +0200191 * For more about ABCs, see the :mod:`abc` module and :pep:`3119`.