adding an attribute to an nd-array

Travis Oliphant oliphant.travis at ieee.org
Thu Oct 19 09:25:36 CDT 2006


Stefan van der Walt wrote:
> On Wed, Oct 18, 2006 at 09:17:49PM -0400, Pierre GM wrote:
>   
>> On Wednesday 18 October 2006 20:29, Stefan van der Walt wrote:
>>     
>>> A quick question on extending numpy arrays: is it possible to easily
>>> add an attribute to an ndarray?
>>>       
>> It might be easier to create a subclass: pleasehave a look here:
>> http://projects.scipy.org/scipy/numpy/attachment/wiki/MaskedArray/test_subclasses.py
>> That's a tiny example of subclassing ndarrays, with some extra attributes.
>> (BTW, I'm not sure that's the most obvious place where to look: if it turns 
>> out to be useful, I'll put it on the scipy wiki)
>>     
>
> Thanks very much, Pierre.
>
> 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)
>
> When does __array_finalize__ get called, and is it always necessary to
> specify it?
>   
It gets called whenever a new array is created.  No, it is not necessary 
to specify it.

-Travis


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