adding an attribute to an nd-array

Stefan van der Walt stefan at sun.ac.za
Thu Oct 19 19:19:37 CDT 2006


On Thu, Oct 19, 2006 at 09:45:02AM -0600, Travis Oliphant wrote:
> Stefan van der Walt wrote:
> 
> >If I understand correctly, the following should work:
> >
> >import numpy as N
> >
> >class InfoArray(N.ndarray):
> >    def __new__(info_arr_cls,arr,info={}):
> >        info_arr_cls.info = info
> >        return N.array(arr).view(info_arr_cls)

One has to be careful of this approach.  It ads *the same* information
to all arrays, i.e.

In [2]: a = ImageInfo(N.array([1,2,3]),{1:1})

In [3]: b = ImageInfo(N.array([1,2,3]),{1:2})

In [4]: a
Out[4]: ImageInfo([1, 2, 3])

In [5]: b
Out[5]: ImageInfo([1, 2, 3])

In [6]: a.info
Out[6]: {1: 2}

In [7]: b.info
Out[7]: {1: 2}

Regards
Stéfan

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list