[Numpy-discussion] simple subclassing of ndarray

Travis Oliphant oliphant.travis at ieee.org
Wed Feb 22 19:28:02 EST 2006

Zachary Pincus wrote:

> Hello folks,
> I'm interested in creating a simple subclass of ndarray that just has  
> a few additional methods. I've stared at defmatrix.py, but I'm not  
> sure what is necessary to do.
> Specifically, I'm not sure how to get new instances of my subclass  
> created properly.
> e.g.:
> numpy.matrix([1,2,3])
> Out: matrix([[1, 2, 3]])
> class m(numpy.ndarray):
>   pass

This is enough to define your own sub-class.  Now, you need to determine 
what you want to do.

You need to understand that m() is now analagous to numpy.ndarray()  so 
you should look at the numpy.ndarray()  docstring for the default 

The array() constructor is not the same thing as ndarray.__new__.

Look at the ndarray docstring.


You need to define the __new__ method *not* the __init__ method.   You 
could, of course, define an __init__ method if you want to, it's just 
not necessary.

> Out: m([[[    13691,         0,         0],
>         [   196608, 296292267, 296303312]]])

You just created an empty array of shape (1,2,3).  The first argument to 
the default constructor is the shape.  

> So clearly I need something else. Looking at the matrix class, it  
> looks like I need a custom __new__ operator. 

Yes, that is exactly right.

> Or perhaps there's a different and better way to construct instances  
> of my subclass? Something akin to the 'array' function would be  
> perfect. Now, how do I go about creating such a function (or getting  
> 'array' to do it)?

You could do


to get instances of your subclass.  This will not call __new__ or 
__init__, but it will call __array_finalize__(self, obj) where obj is 
the ndarray constructed from [1,2,3].

Actually __array_finalize__ is called every time a sub-class is 
constructed and so it could be used to pass along meta-data (or enforce 
rank-2 as it does in the matrix class).


More information about the NumPy-Discussion mailing list