[Python-Dev] bytes-like objects

Nick Coghlan ncoghlan at gmail.com
Mon Oct 6 05:36:08 CEST 2014

On 6 October 2014 07:24, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> anatoly techtonik wrote:
>> That's a cool stuff. `bytes-like object` is really a much better name for
>> users.
> I'm not so sure. Usually when we talk about an "xxx-like object" we
> mean one that supports a certain Python interface, e.g. a "file-like
> object" is one that has read() and/or write() methods. But you can't
> create an object that supports the buffer protocol by implementing
> Python methods.
> I'm worried that using the term "bytes-like object" will lead
> people to ask "What methods do I have to implement to make my
> object bytes-like?", to which the answer is "mu".

That's a defect in the language definition, which requires volunteers
willing and able to work through the task of defining and gathering
consensus around a Python level counterpart to PEP 3118 that works
across arbitrary Python implementations:

Ideally the drive for this would come from the Cython, PyPy,
IronPython and Jython communities, as they would all likely benefit
from a C independent way to express support for the buffer protocol.
Enabling this level of flexibility in defining bytes-like objects is
likely to be a key step in extending deep array oriented programming
support from CPython to other Python implementations.

In the meantime, the answer to "How do I define a bytes-like type?" is:

1. Use the appropriate implementation specific buffer protocols; or
2. Inherit from an existing bytes-like type


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

More information about the Python-Dev mailing list