Movatterモバイル変換


[0]ホーム

URL:


Navigation

8.4.collections.abc — Abstract Base Classes for Containers

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.

8.4.1. Collections Abstract Base Classes

The collections module offers the followingABCs:

ABCInherits fromAbstract MethodsMixin Methods
Container __contains__ 
Hashable __hash__ 
Iterable __iter__ 
IteratorIterable__next____iter__
Sized __len__ 
Callable __call__ 
SequenceSized,Iterable,Container__getitem__,__len____contains__,__iter__,__reversed__,index, andcount
MutableSequenceSequence__getitem__,__setitem__,__delitem__,__len__,insertInheritedSequence methods andappend,reverse,extend,pop,remove, and__iadd__
SetSized,Iterable,Container__contains__,__iter__,__len____le__,__lt__,__eq__,__ne__,__gt__,__ge__,__and__,__or__,__sub__,__xor__, andisdisjoint
MutableSetSet__contains__,__iter__,__len__,add,discardInheritedSet methods andclear,pop,remove,__ior__,__iand__,__ixor__, and__isub__
MappingSized,Iterable,Container__getitem__,__iter__,__len____contains__,keys,items,values,get,__eq__, and__ne__
MutableMappingMapping__getitem__,__setitem__,__delitem__,__iter__,__len__InheritedMapping methods andpop,popitem,clear,update,andsetdefault
MappingViewSized __len__
ItemsViewMappingView,Set __contains__,__iter__
KeysViewMappingView,Set __contains__,__iter__
ValuesViewMappingView __contains__,__iter__
classcollections.abc.Container
classcollections.abc.Hashable
classcollections.abc.Sized
classcollections.abc.Callable

ABCs for classes that provide respectively the methods__contains__(),__hash__(),__len__(), and__call__().

classcollections.abc.Iterable

ABC for classes that provide the__iter__() method.See also the definition ofiterable.

classcollections.abc.Iterator

ABC for classes that provide the__iter__() and__next__() methods. See also the definition ofiterator.

classcollections.abc.Sequence
classcollections.abc.MutableSequence

ABCs for read-only and mutablesequences.

classcollections.abc.Set
classcollections.abc.MutableSet

ABCs for read-only and mutable sets.

classcollections.abc.Mapping
classcollections.abc.MutableMapping

ABCs for read-only and mutablemappings.

classcollections.abc.MappingView
classcollections.abc.ItemsView
classcollections.abc.KeysView
classcollections.abc.ValuesView

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:

  1. Since some set operations create new sets, the default mixin methods needa way to create new instances from an iterable. The class constructor isassumed to have a signature in the formClassName(iterable).That assumption is factored-out to an internal classmethod called_from_iterable() which callscls(iterable) to produce a new set.If theSet mixin is being used in a class with a differentconstructor signature, you will need to override_from_iterable()with a classmethod that can construct new instances froman iterable argument.
  2. To override the comparisons (presumably for speed, as thesemantics are fixed), redefine__le__() andthen the other operations will automatically follow suit.
  3. TheSet mixin provides a_hash() method to compute a hash valuefor the set; however,__hash__() is not defined because not all setsare hashable or immutable. To add set hashabilty using mixins,inherit from bothSet() andHashable(), then define__hash__=Set._hash.

See also

Table Of Contents

Previous topic

8.3.collections — Container datatypes

Next topic

8.5.heapq — Heap queue algorithm

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp