collections.abc -- official
提供了一些抽象基础类,用于判断某个类是否含有某个接口。
抽象基础类有几个最原始的抽象类型:
(1)容器 -- Container
(2)可哈希 -- Hashable
(3)容量 -- Sized
(4)可调用 -- Callable
(5)可迭代 -- Iterable
实际上对应几个抽象接口
其它的任何抽象数据类型, 都是继承了这几个原始的抽象数据类型。
https://docs.python.org/3.5/library/collections.abc.html
This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.
he collections module offers the following ABCs:
ABC Inherits from Abstract Methods Mixin Methods Container
__contains__
Hashable
__hash__
Iterable
__iter__
Iterator
Iterable
__next__
__iter__
Generator
Iterator
send
,throw
close
,__iter__
,__next__
Sized
__len__
Callable
__call__
Sequence
Sized
,Iterable
,Container
__getitem__
,__len__
__contains__
,__iter__
,__reversed__
,index
, andcount
MutableSequence
Sequence
__getitem__
,__setitem__
,__delitem__
,__len__
,insert
Inherited Sequence
methods andappend
,reverse
,extend
,pop
,remove
, and__iadd__
ByteString
Sequence
__getitem__
,__len__
Inherited Sequence
methodsSet
Sized
,Iterable
,Container
__contains__
,__iter__
,__len__
__le__
,__lt__
,__eq__
,__ne__
,__gt__
,__ge__
,__and__
,__or__
,__sub__
,__xor__
, andisdisjoint
MutableSet
Set
__contains__
,__iter__
,__len__
,add
,discard
Inherited Set
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__
Inherited Mapping
methods andpop
,popitem
,clear
,update
, andsetdefault
MappingView
Sized
__len__
ItemsView
MappingView
,Set
__contains__
,__iter__
KeysView
MappingView
,Set
__contains__
,__iter__
ValuesView
MappingView
__contains__
,__iter__
Awaitable
__await__
Coroutine
Awaitable
send
,throw
close
AsyncIterable
__aiter__
AsyncIterator
AsyncIterable
__anext__
__aiter__
collections.abc -- introduction
每种抽象数据类型的目的即使如下
任何collections中具体的数据类型, 和 内置的数据类型, 例如 list set, 都是抽象数据类型的子类。
https://pymotw.com/3/collections/abc.html
The
collections.abc
module contains abstract base classes that define the APIs for container data structures built into Python and provided by thecollections
module.Refer to the table below for a list of the classes and their purposes.
Class Base Class(es) API Purpose Container Basic container features, such as the in
operator.Hashable Adds support for providing a hash value for the container instance. Iterable Can create an iterator over the container contents. Iterator Iterable Is an iterator over the container contents. Generator Iterator Extends iterators with the generator protocol from PEP 342. Sized Adds methods for containers that know how big they are. Callable For containers that can be invoked as a function. Sequence Sized, Iterable, Container Supports retrieving individual items, iterating, and changing the order of items. MutableSequence Sequence Supports adding and removing items to an instance after it has been created. ByteString Sequence Combined API of bytes
andbytearray
.Set Sized, Iterable, Container Supports set operations such as intersection and union. MutableSet Set Adds methods for manipulating the set contents after it is created. Mapping Sized, Iterable, Container Defines the read-only API used by dict
.MutableMapping Mapping Defines the methods for manipulating the contents of a mapping after it is created. MappingView Sized Defines the view API for accessing a mapping from an iterator. ItemsView MappingView, Set Part of the view API. KeysView MappingView, Set Part of the view API. ValuesView MappingView Part of the view API. Awaitable API for objects that can be used in await
expressions, such as coroutines.Coroutine Awaitable API for classes that implement the coroutine protocol. AsyncIterable API for iterables compatible with async for
, as defined in PEP 492.AsyncIterator AsyncIterable API for asynchronous iterators.
继承关系图
https://bugs.python.org/issue32471
dict API
从词典的定义中,我们发现其含有 container接口, sized接口, hashable接口。
class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ def clear(self): # real signature unknown; restored from __doc__ """ D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(*args, **kwargs): # real signature unknown """ Create a new dictionary with keys from iterable and values set to value. """ pass def get(self, *args, **kwargs): # real signature unknown """ Return the value for key if key is in the dictionary, else default. """ pass def items(self): # real signature unknown; restored from __doc__ """ D.items() -> a set-like object providing a view on D's items """ pass def keys(self): # real signature unknown; restored from __doc__ """ D.keys() -> a set-like object providing a view on D's keys """ pass def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised """ pass def popitem(self): # real signature unknown; restored from __doc__ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, *args, **kwargs): # real signature unknown """ Insert key with a value of default if key is not in the dictionary. Return the value for key if key is in the dictionary, else default. """ pass def update(self, E=None, **F): # known special case of dict.update """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ pass def values(self): # real signature unknown; restored from __doc__ """ D.values() -> an object providing a view on D's values """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ True if the dictionary has the specified key, else False. """ pass def __delitem__(self, *args, **kwargs): # real signature unknown """ Delete self[key]. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __init__(self, seq=None, **kwargs): # known special case of dict.__init__ """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __setitem__(self, *args, **kwargs): # real signature unknown """ Set self[key] to value. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass __hash__ = None