[Numpy-discussion] Array Protocol change for Python 2.6

David M. Cooke cookedm at physics.mcmaster.ca
Fri Jun 9 19:30:57 EDT 2006

On Fri, 09 Jun 2006 16:03:32 -0700
Andrew Straw <strawman at astraw.com> wrote:

> Tim Hochberg wrote:
> >Which of the following should we require for an object to be "supporting 
> >the array interface"? Here a producer is something that supplies 
> >array_struct or array_interface (where the latter is the Python level 
> >version of the former as per recent messages). Consumers do something 
> >with the results.
> >
> >   1. Producers can supply either array_struct (if implemented in C) or
> >      array_interface (if implemented in Python). Consumers must accept
> >      both.
> >   2. Producers must supply both array_struct and array_interface.
> >      Consumers may accept either.
> >   3. Producers most supply both array_struct and array_interface.
> >      Consumers must accept both as well.
> >  
> >
> I haven't been following as closely as I could, but is the following a 
> possibility?
>     4. Producers can supply either array_struct or array_interface. 
> Consumers may accept either. The intermediate is a small, standalone 
> (does not depend on NumPy) extension module that does automatic 
> translation if necessary by provides 2 functions: as_array_struct() 
> (which returns a CObject) and as_array_interface() (which returns a 
> tuple/dict/whatever).

For something to go in the Python standard library this is certainly
possible. Heck, if it's in the standard library we can have one attribute
which is a special ArrayInterface object, which can be queried from both
Python and C efficiently.

For something like numpy (where we don't require a special object: the
"producer" and "consumers" in Tim's terminology could be Numeric and
numarray, for instance), we don't want a 3rd-party dependence. There's one
case that I mentioned in another email:

5. Producers must supply array_interface, and may supply array_struct.
Consumers can use either.

Requiring array_struct means that Python-only modules can't play along, so I
think it should be optional (of course, if you're concerned about speed, you
would provide it).

Or maybe we should revisit the "no external dependencies". Perhaps one module
would make everything easier, with helper functions and consistent handling
of special cases. Packages wouldn't need it if they don't interact: you could
conditionally import it when __array_interface__ is requested, and fail if
you don't have it. It would just be required if you want to do sharing.

|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca

More information about the NumPy-Discussion mailing list