New in version 3.3:Formerly, this module was part of thecollections module.
Source code:Lib/collections/abc.py
This module providesabstract base classes thatcan be used to test whether a class provides a particular interface; forexample, whether it is hashable or whether it is a mapping.
The collections module offers the followingABCs:
| ABC | Inherits from | Abstract Methods | Mixin Methods |
|---|---|---|---|
| Container | __contains__ | ||
| Hashable | __hash__ | ||
| Iterable | __iter__ | ||
| Iterator | Iterable | __next__ | __iter__ |
| Sized | __len__ | ||
| Callable | __call__ | ||
| Sequence | Sized,Iterable,Container | __getitem__,__len__ | __contains__,__iter__,__reversed__,index, andcount |
| MutableSequence | Sequence | __getitem__,__setitem__,__delitem__,__len__,insert | InheritedSequence methods andappend,reverse,extend,pop,remove, and__iadd__ |
| Set | Sized,Iterable,Container | __contains__,__iter__,__len__ | __le__,__lt__,__eq__,__ne__,__gt__,__ge__,__and__,__or__,__sub__,__xor__, andisdisjoint |
| MutableSet | Set | __contains__,__iter__,__len__,add,discard | InheritedSet methods andclear,pop,remove,__ior__,__iand__,__ixor__, and__isub__ |
| Mapping | Sized,Iterable,Container | __getitem__,__iter__,__len__ | __contains__,keys,items,values,get,__eq__, and__ne__ |
| MutableMapping | Mapping | __getitem__,__setitem__,__delitem__,__iter__,__len__ | InheritedMapping methods andpop,popitem,clear,update,andsetdefault |
| MappingView | Sized | __len__ | |
| ItemsView | MappingView,Set | __contains__,__iter__ | |
| KeysView | MappingView,Set | __contains__,__iter__ | |
| ValuesView | MappingView | __contains__,__iter__ |
ABCs for classes that provide respectively the methods__contains__(),__hash__(),__len__(), and__call__().
ABC for classes that provide the__iter__() method.See also the definition ofiterable.
ABC for classes that provide the__iter__() and__next__() methods. See also the definition ofiterator.
ABCs for read-only and mutablesequences.
ABCs for read-only and mutablemappings.
ABCs for mapping, items, keys, and valuesviews.
These ABCs allow us to ask classes or instances if they provideparticular functionality, for example:
size=Noneifisinstance(myvar,collections.abc.Sized):size=len(myvar)
Several of the ABCs are also useful as mixins that make it easier to developclasses supporting container APIs. For example, to write a class supportingthe fullSet API, it only necessary to supply the three underlyingabstract methods:__contains__(),__iter__(), and__len__().The ABC supplies the remaining methods such as__and__() andisdisjoint():
classListBasedSet(collections.abc.Set):''' Alternate set implementation favoring space over speed and not requiring the set elements to be hashable. '''def__init__(self,iterable):self.elements=lst=[]forvalueiniterable:ifvaluenotinlst:lst.append(value)def__iter__(self):returniter(self.elements)def__contains__(self,value):returnvalueinself.elementsdef__len__(self):returnlen(self.elements)s1=ListBasedSet('abcdef')s2=ListBasedSet('defghi')overlap=s1&s2# The __and__() method is supported automatically
Notes on usingSet andMutableSet as a mixin:
See also
8.3.collections — Container datatypes
8.5.heapq — Heap queue algorithm
Enter search terms or a module, class or function name.