Python Glossary Русский

all Python 3ioterm

io.BufferedIOBase

abstract bases

The abstract base class for buffered binary streams (BytesIO, BufferedReader/Writer, …).

Syntax

issubclass(cls, io.BufferedIOBase)

The practical use of this ABC is telling a buffered binary stream from a text one: open(path, 'rb') gives a BufferedReader while open(path) gives a TextIOWrapper, and an isinstance check against the base class beats poking at the mode attribute. On buffered streams read(n) returns exactly n bytes until it hits EOF, unlike raw RawIOBase streams where a short read is legal at any time.

Examples

import io
print(issubclass(io.BytesIO, io.BufferedIOBase))   # → True
print(issubclass(io.BufferedReader, io.BufferedIOBase))   # → True
print(issubclass(io.StringIO, io.BufferedIOBase))   # → False
print(issubclass(io.BufferedIOBase, io.IOBase))   # → True
buf = io.BytesIO(b'hello')
print(isinstance(buf, io.BufferedIOBase))   # → True
print(io.BufferedIOBase().read())   # → io.UnsupportedOperation

See also

Open in the full glossarydocs.python.orgReport an inaccuracy