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