[Python-Dev] PEP 298, __buffer__

Scott Gilbert xscottg@yahoo.com
Thu, 1 Aug 2002 22:54:12 -0700 (PDT)

Tonight, I remember another thought that I've had for a while.

There isn't currently a way for a class object created from Python script
to indicate that it wishes to implement the buffer interface.  In the
Numeric source, I've seen them use self.__buffer__ for this purpose, but
this isn't actually an officially sanctioned magic name.

Now that classes can derive from builtin types, perhaps there is less of a
need for this, but I still think we would want it.  There are times when
you want inheritance, and others when you want containment.  With a slight
modification to the PyObject_*Buffer functions (in the failure branches),
an instance of a class could use containment of a PyBufferProcs supporting
object and publish the buffer interface as its own.

I'm thinking one of:

    class OneWay(object):
        def __init__(self):
            self.__buffer__ = bytes(1000)


    class SomeOther(object):
        def __init__(self):
            self._private = bytes(1000)
        def __buffer__(self):
            return self._private
I believe the first one is the way it's done in Numeric (Numarray too?). 
(Maybe Todd Miller will comment on this and whether it's useful to him.)

If this is worthwhile, it could be added to PEP 298 or as a new mini PEP. 
In either case, I'm willing to do the work.


Do You Yahoo!?
Yahoo! Health - Feel better, live better