Python 3.12+collectionsterm
collections.abc.Buffer
collections.abc — protocols
The buffer protocol: an object exposing its memory (3.12+).
Syntax
isinstance(obj, collections.abc.Buffer)
The buffer protocol used to live only at the C level, so before 3.12 there was no way to test for it from Python; PEP 688 added the __buffer__ method, and a plain Python class can now qualify as a Buffer. A surprise for many: str is not a buffer, even though bytes can be derived from it via an explicit encode(). As an annotation, Buffer is the proper successor to the deprecated ByteString whenever a function accepts any raw-bytes object, memoryview and array included.
Examples
import collections.abc as abc
print(isinstance(b'x', abc.Buffer)) # → True
print(isinstance(bytearray(b'x'), abc.Buffer)) # → True
print(isinstance(memoryview(b'abc'), abc.Buffer)) # → True
print(issubclass(bytes, abc.Buffer)) # → True
print(isinstance('hello', abc.Buffer)) # → False
See also
Open in the full glossarydocs.python.orgReport an inaccuracy