[Numpy-discussion] numpy subclass

Matthew Brett matthew.brett at gmail.com
Thu Jul 23 16:33:53 EDT 2009


Hi,

> In short, I want to create a class Dummy, inherited from np.ndarray, which
> returns a plain array whenever an instance is sliced or viewed. I cannot figure
> out how.

> class Dummy(np.ndarray):
>
> def __new__(cls, array):
>   obj=array.view(cls)
>   return obj
> def __array_finalize__(self, obj):
>   #self=self.view(np.ndarray)
>   #self=np.asarray(self)

I think the problem here is that self.view(np.ndarray) returns another
object, which is of type ndarray, pointing to the data in self.  The
problem being that you have not changed the object pointed to by
``self``, you've only made the local variable ``self`` point to
something different that will then be thrown away when your new
``self`` variable goes out of scope.

What you want to do here, is to modify the class of ``self`` inplace;
I don't know how to do that...

Best,

Matthew



More information about the NumPy-Discussion mailing list