@songying
2018-11-14T14:25:04.000000Z
字数 2505
阅读 1200
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 |
class Container(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __contains__(self, x):
return False
@classmethod
def __subclasshook__(cls, C):
if cls is Container:
if any("__contains__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
class Hashable(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __hash__(self):
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is Hashable:
for B in C.__mro__:
if "__hash__" in B.__dict__:
if B.__dict__["__hash__"]:
return True
break
return NotImplemented
class Iterable(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __iter__(self):
while False:
yield None
@classmethod
def __subclasshook__(cls, C):
if cls is Iterable:
if any("__iter__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
class Iterator(Iterable):
__slots__ = ()
@abstractmethod
def __next__(self):
'Return the next item from the iterator. When exhausted, raise StopIteration'
raise StopIteration
def __iter__(self):
return self
@classmethod
def __subclasshook__(cls, C):
if cls is Iterator:
if (any("__next__" in B.__dict__ for B in C.__mro__) and
any("__iter__" in B.__dict__ for B in C.__mro__)):
return True
return NotImplemented
class Sized(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __len__(self):
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is Sized:
if any("__len__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
class Callable(metaclass=ABCMeta):
__slots__ = ()
@abstractmethod
def __call__(self, *args, **kwds):
return False
@classmethod
def __subclasshook__(cls, C):
if cls is Callable:
if any("__call__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented