5.3. Abstract Builtin ABC

  • from collections.abc import *

  • Container

  • Hashable

  • Iterable

  • Iterator

  • Reversible

  • Generator

  • Callable

  • Collection

  • Sequence

  • MutableSequence

  • ByteString

  • Set

  • MutableSet

  • Mapping

  • MutableMapping

  • MappingView

  • ItemsView

  • KeysView

  • ValuesView

  • Awaitable

  • Coroutine

  • AsyncIterator

  • AsyncGenerator

Table 5.6. Protocols

Protocol

Methods

Callable

__call__

Container

__contains__

Hashable

__hash__

Reversible

__reversed__

Iterable

__iter__

Iterator

__iter__, __next__

Generator

__iter__, __next__, close, send, throw

Collection

__contains__, __iter__, __len__

Sequence

__contains__, __getitem__, __iter__, __len__, __reversed__, count,``index``

MutableSequence

__contains__, __delitem__, __getitem__, __iadd__, __iter__, __len__, __reversed__, __setitem__, append, count, extend, index, insert, pop, remove, reverse

ByteString

__contains__, __getitem__, __iter__, __len__, __reversed__, count, index

Set

__and__, __contains__, __eq__, __ge__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __or__, __sub__, __xor__, isdisjoint

MutableSet

__and__, __contains__, __contains__, __eq__, __ge__, __gt__, __iand__, __ior__, __isub__, __iter__, __iter__, __ixor__, __le__, __len__, __len__, __lt__, __ne__, __or__, __sub__, __xor__, add, clear, discard, isdisjoint,``pop``, remove

Mapping

__contains__, __eq__, __getitem__, __iter__, __len__, __ne__, get, items, keys, values

MutableMapping

__contains__, __delitem__, __eq__, __getitem__, __getitem__, __iter__, __iter__, __len__,``__len__``, __ne__, __setitem__, clear, get, items, keys, pop, popitem, setdefault, update, values,

MappingView

__len__

ItemsView

__contains__, __iter__

KeysView

__contains__, __iter__

ValuesView

__contains__, __iter__

Awaitable

__await__

Coroutine

__aiter__, close, send, throw

AsyncIterator

__aiter__, __anext__

AsyncGenerator

__aiter__, __anext__, aclose, asend, athrow

5.3.1. Use Case - 1

>>> data = range(0,3)
>>>
>>>
>>> hasattr(data, '__iter__')
True
>>>
>>> hasattr(data, '__next__')
False
>>> result = iter(data)
>>>
>>>
>>> hasattr(result, '__iter__')
True
>>>
>>> hasattr(result, '__next__')
True

5.3.2. Use Case - 2

>>> result = enumerate(['a', 'b', 'c'])
>>>
>>>
>>> hasattr(result, '__iter__')
True
>>>
>>> hasattr(result, '__next__')
True