[Python-Dev] PEP 298, __buffer__

Todd Miller jmiller@stsci.edu
Fri, 02 Aug 2002 06:41:05 -0400


Scott Gilbert wrote:

>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.
>
>
>I'm thinking one of:
>
>    class OneWay(object):
>        def __init__(self):
>            self.__buffer__ = bytes(1000)
>
>Or:
>
>    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?). 
>
The numarray C-API essentially supports both usages, although we only 
use the __buffer__ name in the second case.  

>
>(Maybe Todd Miller will comment on this and whether it's useful to him.)
>
Yes, it is useful for prototyping.   Numarray calls a  __buffer__() 
method to support python class wrappers around mmap.   We use our class 
wrappers around mmap to add the ability to chop a file up into 
non-overlapping resizeable slices.  Each slice can be used as the buffer 
of an independent memory mapped array.

Todd