Stefan van der Walt <stefan at sun.ac.za>

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


> > >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.

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 = obj.info
        else:
            self.info = self._info
        return

OK, so you end up w/ two attributes 'info' and '_info', the latter having
the
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.info=={1:1}
assert b.info=={1:2}
assert (a+1).info==a.info
assert (b-2).info==b.info
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061019/0cce0d07/attachment-0001.html 
-------------- 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
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
-------------- next part --------------
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list