[Numpy-discussion] ndarray subclassing
Thu May 1 00:15:55 CDT 2008
On Thu, May 1, 2008, Travis E. Oliphant wrote:
> You are just seeing the result of __repr__. The printing code works by
> accessing slices of the array. These slices create new instances of
> your TestArray class which have a smaller number of dimensions. That's all.
Ahh, that makes sense. Thanks so much for the quick reply!
> Is the printing code causing you other kinds of problems?
The problem was that my __array_finalize__ code did some checking on
the shape of the array. In short, one of the attributes of my class is
a list that has one entry for each dimension. __array_finalize__
checks to make sure this list length and the number of dimensions
match and it throws an exception if they don't. This leads to an
exception being thrown whenever I try to print the contents of an
array with more than 1 dimensions, because I haven't yet implemented a
__getitem__ method that adjusts this attribute when slices are taken.
I didn't realize that this was what's going on and was very puzzled by
the results. It looks like I can make things work by using _getitem
flag as is done in the matrix class.
Thanks again for clearing this up! I think it would be great if
somebody with write access to the wiki could make a note of this on
the sublasses page: http://www.scipy.org/Subclasses
More information about the Numpy-discussion