From: Stefan v. d. W. <st...@su...> - 2006-10-19 11:21:12
|
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? >=20 > 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 attribut= es. > (BTW, I'm not sure that's the most obvious place where to look: if it t= urns=20 > 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=3D{}): info_arr_cls.info =3D info return N.array(arr).view(info_arr_cls) When does __array_finalize__ get called, and is it always necessary to specify it? Cheers St=E9fan |