Python Glossary Русский

all Python 3ioterm

io.TextIOBase

abstract bases

The abstract base class for text streams (StringIO, an opened text file); works with str.

Syntax

issubclass(cls, io.TextIOBase)

You never instantiate this class; it exists for isinstance/issubclass checks and as a base for custom text streams. The concrete text streams are StringIO and TextIOWrapper, and the latter is what open() hands back in text mode, as well as what sys.stdin and sys.stdout are. The practical consequence is the str/bytes split: a text stream accepts only str and raises TypeError on bytes, while binary streams such as BytesIO or BufferedReader accept only bytes, and only the text side gives you encoding and newline translation.

Examples

import io
print(issubclass(io.StringIO, io.TextIOBase))   # → True
print(issubclass(io.TextIOWrapper, io.TextIOBase), issubclass(io.BytesIO, io.TextIOBase))   # → True False
s = io.StringIO('hello')
print(isinstance(s, io.TextIOBase), s.read())   # → True hello
print(io.StringIO().write(b'x'))   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy