adding an attribute to an nd-array

Stefan van der Walt stefan at sun.ac.za
Fri Oct 20 03:47:05 CDT 2006


On Thu, Oct 19, 2006 at 09:03:57PM -0400, Pierre GM wrote:
> 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.

Is there any reason why one can't simply do

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

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