Native object exposing buffer protocol

Ben Finney ben+python at benfinney.id.au
Fri Jan 5 19:27:04 EST 2018


Rob Gaddi <rgaddi at highlandtechnology.invalid> writes:

> I'd like to create a native Python object that exposes the buffer
> protocol.  Basically, something with a ._data member which is a
> bytearray that I can still readinto, make directly into a numpy array,
> etc.

The “etc.” seems pretty important, there. You want the behaviour of
‘bytearray’ without actually inheriting that behaviour from the
‘bytearray’ type.

So, it seems your options are:

* Enumerate all the things, specifically, that you do want your new type
  to do. Don't hide anything in “etc.”, so that you know exactly what
  behaviours need to be implemented. Implement all those behaviours,
  without benefit of inheriting from ‘bytearray’.

* Inherit from ‘bytearray’, but ameliorate the problems you want to
  avoid. This will require enumerating all those problems, so that you
  can know whether you have avoided them. Don't hide any of them in an
  “etc.”.

> Not the end of the world (the file's less than 2KB), but it seems like
> something that should be doable easily without having to throw around
> a lot of extraneous copies.

I look forward to your report from having tried it :-)

-- 
 \      “A lie can be told in a few words. Debunking that lie can take |
  `\   pages. That is why my book… is five hundred pages long.” —Chris |
_o__)                                                Rodda, 2011-05-05 |
Ben Finney




More information about the Python-list mailing list