Stefan van der Walt <stefan at>

P GM pgmdevlist at
Thu Oct 19 20:06:23 CDT 2006

> > >class InfoArray(N.ndarray):
> > >    def __new__(info_arr_cls,arr,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.

Indeed. That's basically why you have to edit your __array_finalize__ .

class InfoArray(N.ndarray):
    def __new__(info_arr_cls,arr,info={}):
        info_arr_cls._info = info
        return N.array(arr).view(info_arr_cls)
    def __array_finalize__(self, obj):
        if hasattr(obj,'info'):
   = self._info

OK, so you end up w/ two attributes 'info' and '_info', the latter having
info you want, the latter playing a temporary placeholder. That looks a bit
overkill, but that works pretty nice.

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

assert (a+1)
assert (b-2)
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
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
-------------- next part --------------
Numpy-discussion mailing list
Numpy-discussion at

More information about the Numpy-discussion mailing list