From: Travis O. <oli...@ie...> - 2006-10-19 14:24:42
|
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 |