Python Glossary Русский

Python 3.5+typingterm

typing.BinaryIO

I/O streams

The type for binary file streams (opened in 'b' mode) in annotations.

Syntax

def f(stream: typing.BinaryIO) -> None: ...

It is effectively IO[bytes]: the kind of object open(path, 'rb') returns, working with bytes rather than str — text streams have a separate TextIO. You do not instantiate or subclass it; it exists only as a type annotation.

Examples

from typing import IO, BinaryIO
import io
print(BinaryIO.__name__)   # → BinaryIO
print(issubclass(BinaryIO, IO))   # → True
from typing import IO, BinaryIO
import io
def read_head(f: BinaryIO) -> bytes: return f.read(4)
print(read_head.__annotations__['f'] is BinaryIO)   # → True
print(read_head(io.BytesIO(b'binary data')))   # → b'bina'
print(isinstance(io.BytesIO(), BinaryIO))   # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy