[Python-Dev] bytes-like objects

Nick Coghlan ncoghlan at gmail.com
Mon Oct 6 05:26:28 CEST 2014

On 6 October 2014 07:32, Victor Stinner <victor.stinner at gmail.com> wrote:
> Hi,
> I prefer "bytes-like" than "buffer protocol". By the way, is there a
> documentation in Python doc which explains "bytes-like" and maybe list most
> compatible types?


It only specifically lists bytes, bytearray and memoryview.

It also links to the current formal definition of what constitutes a
bytes like object:

memoryview.cast() offers a lot of capabilities to write interesting
buffer exporters in pure Python, but it's currently not possible as
there's no standard API defined for doing so:

PyPy created a private Python level protocol to allow bytes-like
objects to be defined in RPython, but their approach hasn't been
proposed for standardisation (it's also not clear whether or not their
internal protocol offers the full range of features offered by PEP

> I'm not sure that the term has an unique definition. In some parts of
> Python, I saw explicit checks on the type: bytes or bytearray, sometimes
> memoryview is accepted. The behaviour is different in C functions using
> PyArg API. It probably depends if the function relies on the content (read
> bytes) or on methods (ex: call .find).

The core practical definition of a "bytes-like object" is whether or
not "memoryview(obj)" works. An object can be bytes like without
supporting the full bytes/bytearray API.

Some APIs that theoretically *could* accept arbitrary bytes like
objects are currently more limited - expanding those cases to accept
arbitrary bytes-like objects is generally considered a minor RFE
rather than a bug fix.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list