2
class memoryview(buffer):
    def tobytes(self):
        return self

buf = memoryview('23')
buf.tobytes()

python interpreter gives me the following error

TypeError: Error when calling the metaclass bases
    type 'buffer' is not an acceptable base type

Why it is so

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
user634615
  • 647
  • 7
  • 17

1 Answers1

1

See the question linked in the comments for the list of possible reasons behind dissallowing this.

The direct technical reason has to do with how Python classes are declared in C. The interesting part is right at the bottom of bufferobject.c:

PyTypeObject PyBuffer_Type = {
    ...
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */
    ...
};

This field, tp_flags, includes a potential setting called Py_TPFLAGS_BASETYPE, which isn't used here. If it was, like it is in, for example, listobject.c, inheriting from buffer would be allowed.

lvc
  • 34,233
  • 10
  • 73
  • 98