collections.abc — 容器抽象基类

Added in version 3.3: Formerly, this module was part of the collections 模块。

源代码: Lib/_collections_abc.py


本模块提供 抽象基类 that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a 映射 .

An issubclass() or isinstance() test for an interface works in one of three ways.

1) A newly written class can inherit directly from one of the abstract base classes. The class must supply the required abstract methods. The remaining mixin methods come from inheritance and can be overridden if desired. Other methods may be added as needed:

class C(Sequence):                      # Direct inheritance
    def __init__(self): ...             # Extra method not required by the ABC
    def __getitem__(self, index):  ...  # Required abstract method
    def __len__(self):  ...             # Required abstract method
    def count(self, value): ...         # Optionally override a mixin method