Python Glossary Русский

Python 3.5+typingterm

typing.SupportsBytes

Supports* protocols

A runtime-checkable protocol for objects supporting bytes() (has __bytes__).

Syntax

isinstance(obj, typing.SupportsBytes)

Watch out: bytes() also accepts an int (bytes(5) → five zero bytes) or an iterable of ints (bytes([1,2,3])), yet neither int nor list defines __bytes__ — so isinstance(5, SupportsBytes) and isinstance([1,2,3], SupportsBytes) are False. The protocol only matches objects with an explicit __bytes__, not everything bytes() can consume.

Examples

from typing import SupportsBytes
class B:
    def __bytes__(self): return b''
print(isinstance(B(), SupportsBytes))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy