[Numpy-discussion] Questions about the array interface.

Scott Gilbert xscottg at yahoo.com
Thu Apr 7 04:43:37 EDT 2005

--- Chris Barker <Chris.Barker at noaa.gov> wrote:
> I can see that it would, but then, we're stuck with checking for all 
> these optional attributes. If I don't bother to check for it, one day, 
> someone is going to pass a weird array in with an offset, and a strange 
> bug will show up.

Everyone seems to think that an offset is so weird.  I haven't looked at
the internals of Numeric/scipy.base in a while so maybe it doesn't apply
there.  However, if you subscript an array and return a view to the data,
you need an offset or you need to create a new buffer that encodes the
offset for you.

    A = reshape(arange(9), (3,3))

        0, 1, 2
        3, 4, 5
        6, 7, 8

    B = A[2]    # create a view into A

        6, 7, 8 # Shared with the data above

Unless you're going to create a new buffer (which I guess is what Numeric
is doing), the offset for B would be 6 in this very simple case.  I think
specifying the offset is much more elegant than creating a new buffer
object with a hidden offset that refers to the old buffer object.

I guess all I'm saying is that I wouldn't assume the offset is zero...

> Couldn't it be required, and return a reference to itself if that works?
> Maybe I'm just being lazy, but it feels clunky and prone to errors to 
> keep having to check if a attribute exists, then use it (or not).

The problem is that you aren't being lazy enough.  :-)

The fact that a lot of these attributes are optional should be hidden in
helper functions like those in Travis's array_interface.py module, or a
C/C++ include file (with inline functions).

In a short while, you shouldn't have to check any __array_metadata__
attributes directly.  There should even be a helper function for getting
the array elements.

It wouldn't be a horrible mistake to have all the attributes be mandatory,
but it doesn't get array consumes any benefit that they can't get from a
well written helper library, and it does add some burden to array


More information about the NumPy-Discussion mailing list