[关闭]
@songying 2018-11-14T14:25:04.000000Z 字数 2505 阅读 1200

collections.abc -- 抽象基类

python库


参考: https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterator
https://hg.python.org/cpython/file/3.4/Lib/_collections_abc.py#l93

类名 继承于 抽象方法 继承方法
Container __contains__
Hashable __hash__
Iterable __iter__
Sized __len__
Callable __call__
Iterator Iterable __next__ __iter__
Reversible Iterable __reversed__ __iter__
Generator Iterator send, throw close, __iter__, __next__
Collection Sized, Iterable, Container __contains__, __iter__, __len__
Sequence Reversible, Collection __getitem__, __len__ __contains__, __iter__, __reversed__, index, and count
MutableSequence Sequence __getitem__, __setitem__, __delitem__, __len__, insert Inherited Sequence methods and append, reverse, extend, pop, remove, and __iadd__
Set Collection __contains__, __iter__, __len__ __le__, __lt__, __eq__, __ne__, __gt__, __ge__, __and__, __or__, __sub__,__xor__, and isdisjoint
ByteString Sequence __getitem__, __len__ Inherited Sequence methods

Container

  1. class Container(metaclass=ABCMeta):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __contains__(self, x):
  5. return False
  6. @classmethod
  7. def __subclasshook__(cls, C):
  8. if cls is Container:
  9. if any("__contains__" in B.__dict__ for B in C.__mro__):
  10. return True
  11. return NotImplemented

Hashable

  1. class Hashable(metaclass=ABCMeta):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __hash__(self):
  5. return 0
  6. @classmethod
  7. def __subclasshook__(cls, C):
  8. if cls is Hashable:
  9. for B in C.__mro__:
  10. if "__hash__" in B.__dict__:
  11. if B.__dict__["__hash__"]:
  12. return True
  13. break
  14. return NotImplemented

Iterable

  1. class Iterable(metaclass=ABCMeta):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __iter__(self):
  5. while False:
  6. yield None
  7. @classmethod
  8. def __subclasshook__(cls, C):
  9. if cls is Iterable:
  10. if any("__iter__" in B.__dict__ for B in C.__mro__):
  11. return True
  12. return NotImplemented

Iterator

  1. class Iterator(Iterable):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __next__(self):
  5. 'Return the next item from the iterator. When exhausted, raise StopIteration'
  6. raise StopIteration
  7. def __iter__(self):
  8. return self
  9. @classmethod
  10. def __subclasshook__(cls, C):
  11. if cls is Iterator:
  12. if (any("__next__" in B.__dict__ for B in C.__mro__) and
  13. any("__iter__" in B.__dict__ for B in C.__mro__)):
  14. return True
  15. return NotImplemented

sized

  1. class Sized(metaclass=ABCMeta):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __len__(self):
  5. return 0
  6. @classmethod
  7. def __subclasshook__(cls, C):
  8. if cls is Sized:
  9. if any("__len__" in B.__dict__ for B in C.__mro__):
  10. return True
  11. return NotImplemented

Callable

  1. class Callable(metaclass=ABCMeta):
  2. __slots__ = ()
  3. @abstractmethod
  4. def __call__(self, *args, **kwds):
  5. return False
  6. @classmethod
  7. def __subclasshook__(cls, C):
  8. if cls is Callable:
  9. if any("__call__" in B.__dict__ for B in C.__mro__):
  10. return True
  11. return NotImplemented
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注