collections.abc — 容器的抽象基类 — Python 文档

来自菜鸟教程
Python/docs/3.8/library/collections.abc
跳转至:导航、​搜索

collections.abc — 容器的抽象基类

3.3 新功能: 以前,这个模块是 collections 模块的一部分。


源代码: :source:`Lib/_collections_abc.py`



该模块提供了抽象基类,可以用来测试一个类是否提供了特定的接口; 例如,它是否是可散列的或是否是映射。

集合抽象基类

collections 模块提供以下 ABCs

美国广播公司 继承自 抽象方法 混合方法
Container __contains__
Hashable __hash__
Iterable __iter__
Iterator Iterable __next__ __iter__
Reversible Iterable __reversed__
Generator Iterator sendthrow close__iter____next__
Sized __len__
Callable __call__
Collection 大小可迭代容器 __contains____iter____len__
Sequence 可逆收藏 __getitem____len__ __contains____iter____reversed__indexcount
MutableSequence Sequence __getitem____setitem____delitem____len__insert 继承了 Sequence 方法和 appendreverseextendpopremove 和 [ X103X]
ByteString Sequence __getitem____len__ 继承 序列 方法
Set Collection __contains____iter____len__ __le____lt____eq____ne____gt____ge____and__、[ X77X]、__sub____xor__isdisjoint
MutableSet Set __contains____iter____len__adddiscard 继承了 Set 方法和 clearpopremove__ior____iand____ixor__ ] 和 __isub__
Mapping Collection __getitem____iter____len__ __contains__keysitemsvaluesget__eq____ne__
MutableMapping Mapping __getitem____setitem____delitem____iter____len__ 继承了 Mapping 方法和 poppopitemclearupdatesetdefault
MappingView Sized __len__
ItemsView MappingView, Set __contains____iter__
KeysView MappingView, Set __contains____iter__
ValuesView 映射视图集合 __contains____iter__
Awaitable __await__
Coroutine Awaitable sendthrow close
AsyncIterable __aiter__
AsyncIterator AsyncIterable __anext__ __aiter__
AsyncGenerator AsyncIterator asendathrow aclose__aiter____anext__
class collections.abc.Container
提供 __contains__() 方法的类的 ABC。
class collections.abc.Hashable
提供 __hash__() 方法的类的 ABC。
class collections.abc.Sized
提供 __len__() 方法的类的 ABC。
class collections.abc.Callable
提供 __call__() 方法的类的 ABC。
class collections.abc.Iterable

提供 __iter__() 方法的类的 ABC。

检查 isinstance(obj, Iterable) 会检测注册为 Iterable 或具有 __iter__() 方法的类,但不会检测使用 __getitem__() 方法迭代的类。 确定对象是否为 iterable 的唯一可靠方法是调用 iter(obj)

class collections.abc.Collection

用于大小可迭代容器类的 ABC。

3.6 版中的新功能。

class collections.abc.Iterator
ABC 用于提供 __iter__()__next__() 方法的类。 另见迭代器的定义。
class collections.abc.Reversible

ABC 用于还提供 __reversed__() 方法的可迭代类。

3.6 版中的新功能。

class collections.abc.Generator

用于实现 PEP 342 中定义的协议的生成器类的 ABC,它使用 send()throw() 和 扩展迭代器X161X]close() 方法。 另见发电机的定义。

3.5 版中的新功能。

class collections.abc.Sequence
class collections.abc.MutableSequence
class collections.abc.ByteString

用于只读和可变 序列 的 ABC。

实现说明:一些mixin方法,如__iter__()__reversed__()index(),会重复调用底层的__getitem__()方法。 因此,如果 __getitem__() 以恒定访问速度实现,mixin 方法将具有线性性能; 然而,如果底层方法是线性的(就像使用链表一样),mixin 将具有二次性能并且可能需要被覆盖。

3.5 版更改: index() 方法添加了对 stopstart 参数的支持。

class collections.abc.Set

class collections.abc.MutableSet

只读和可变集的 ABC。
class collections.abc.Mapping

class collections.abc.MutableMapping

用于只读和可变 映射 的 ABC。
class collections.abc.MappingView

class collections.abc.ItemsView
class collections.abc.KeysView
class collections.abc.ValuesView

用于映射、项目、键和值 视图 的 ABC。
class collections.abc.Awaitable

awaitable 对象的 ABC,可用于 await 表达式。 自定义实现必须提供 __await__() 方法。

Coroutine对象和CoroutineABC的实例都是这个ABC的实例。

笔记

在 CPython 中,基于生成器的协程(用 types.coroutine()asyncio.coroutine() 修饰的生成器)是 awaitables,即使它们没有__await__() 方法。 对它们使用 isinstance(gencoro, Awaitable) 将返回 False。 使用 inspect.isawaitable() 来检测它们。

3.5 版中的新功能。

class collections.abc.Coroutine

用于协程兼容类的 ABC。 这些实现了在 协程对象 中定义的以下方法:send()throw()close()。 自定义实现还必须实现 __await__()。 所有 Coroutine 实例也是 Awaitable 的实例。 另见协程的定义。

笔记

在 CPython 中,基于生成器的协程(用 types.coroutine()asyncio.coroutine() 修饰的生成器)是 awaitables,即使它们没有__await__() 方法。 对它们使用 isinstance(gencoro, Coroutine) 将返回 False。 使用 inspect.isawaitable() 来检测它们。

3.5 版中的新功能。

class collections.abc.AsyncIterable

提供 __aiter__ 方法的类的 ABC。 另见异步可迭代的定义。

3.5 版中的新功能。

class collections.abc.AsyncIterator

ABC 用于提供 __aiter____anext__ 方法的类。 另见异步迭代器的定义。

3.5 版中的新功能。

class collections.abc.AsyncGenerator

ABC 用于实现 PEP 525PEP 492 中定义的协议的异步生成器类。

3.6 版中的新功能。

这些 ABC 允许我们询问类或实例是否提供特定功能,例如:

size = None
if isinstance(myvar, collections.abc.Sized):
    size = len(myvar)

一些 ABC 也可用作 mixin,可以更轻松地开发支持容器 API 的类。 例如,要编写一个支持完整的 Set API 的类,只需要提供三个底层抽象方法:__contains__()__iter__() 和 [ X168X]。 ABC 提供了其余的方法,例如 __and__()isdisjoint()

class ListBasedSet(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 = []
        for value in iterable:
            if value not in lst:
                lst.append(value)

    def __iter__(self):
        return iter(self.elements)

    def __contains__(self, value):
        return value in self.elements

    def __len__(self):
        return len(self.elements)

s1 = ListBasedSet('abcdef')
s2 = ListBasedSet('defghi')
overlap = s1 & s2            # The __and__() method is supported automatically

使用 SetMutableSet 作为 mixin 的注意事项:

  1. 由于某些集合操作会创建新集合,因此默认的 mixin 方法需要一种从可迭代对象创建新实例的方法。 假定类构造函数具有 ClassName(iterable) 形式的签名。 该假设被分解为称为 _from_iterable() 的内部类方法,该方法调用 cls(iterable) 以生成新集合。 如果在具有不同构造函数签名的类中使用 Set mixin,则需要使用可以从可迭代参数构造新实例的类方法覆盖 _from_iterable()
  2. 要覆盖比较(大概是为了速度,因为语义是固定的),重新定义 __le__()__ge__(),然后其他操作将自动效仿。
  3. Set mixin 提供了一个 _hash() 方法来计算集合的哈希值; 然而,__hash__() 并未定义,因为并非所有集合都是可散列或不可变的。 要使用 mixin 添加 set hashability,请继承 Set()Hashable(),然后定义 __hash__ = Set._hash

也可以看看