[Numpy-discussion] subclassing ndaray

Travis Oliphant oliphant at ee.byu.edu
Mon Feb 27 12:01:06 CST 2006

Christopher Hanley wrote:

> Travis Oliphant wrote:
>> So, this seems like a workable compromise.  It should work a bit 
>> better now that I've changed it so that __array_finalize__ is called 
>> on every sub-class creation.  If there is no "parent" then parent 
>> will be None.
> Hi Travis,
> The change you made just broke the following bit of code:

Right.  It's because obj can sometimes be None...

> class FITS_rec(rec.recarray):
>     def __new__(subtype, input):
>         """Construct a FITS record array from a recarray."""
>         # input should be a record array
>         self = rec.recarray.__new__(subtype, input.shape, input.dtype,
>             buf=input.data, strides=input.strides)
>         self._nfields = len(self.dtype.fields[-1])
>         self._convert = [None]*len(self.dtype.fields[-1])
>         self._coldefs = None
>         return self
>     def __array_finalize__(self,obj):
>         self._convert = obj._convert
>         self._coldefs = obj._coldefs
>         self._nfields = obj._nfields

Add as the first line.

if obj is None:


> Given what you have just said I would not have expected this to be 
> broken.

The change is that now __array_finalize__ is always called (even if 
there is no "parent" in which case obj is None).    This seems easier to 
explain and a bit more consistent. 

Feedback encouraged...


More information about the Numpy-discussion mailing list