[Numpy-discussion] how add new attribute to a numpy array object ?

Eric Firing efiring at hawaii.edu
Sun Jul 23 02:11:59 CDT 2006

Sebastian Haase wrote:
> Hi,
> I have a (medical) image file.
> I wrote a nice interface based on memmap using numarray.
> The class design I used  was essentially to return a numarray array 
> object with a new "custom" attribute giving access to special 
> information about the base file.
> Now with numpy I noticed that a numpy object does not allow adding new 
> attributes !! (How is this ? Why ?)
> Travis already suggested (replying to one of my last postings) to create 
> a new sub class of numpy.ndarray.
> But how do I initialize an object of my new class to be "basically 
> identically to" an existing ndarray object ?
> Normally I could do
> class B(N.ndarray):
>     pass
> a=N.arange(10)
> a.__class__ = B

Isn't this what you need to do instead?

In [1]:import numpy as N

In [2]:class B(N.ndarray):
    ...:    pass

In [3]:a = B(N.arange(10))

In [4]:a.__class__
Out[4]:<class '__main__.B'>

In [5]:a.stuff = 'stuff'

I don't think it makes sense to try to change the __class__ attribute by 


More information about the Numpy-discussion mailing list