Hi Matthew

On Tue, Jan 02, 2007 at 02:30:34AM -0500, Matthew Koichi Grimes wrote:
> Is it hard to subclass recarrays? I'm currently working on a subclass 
> that  doesn't add much; just some methods that sets all the values to 
> zero and other similarly trivial things. It'd be nice to make it work, 
> but if I have to use external functions on a plain recarray instead of 
> this subclass, I won't be found weeping in the corner.

Pierre wrote this useful wiki entry on subclassing:

http://www.scipy.org/Subclasses

It describes exactly what you want to do.

Find attached some example code.

Regards
Stéfan
import numpy as N

class SubRec(N.recarray):
    dtype = N.dtype([('x','f8'),('dx','f8'),('deltas','f8')])

    def __new__(subtype,shape):
        data = N.zeros(shape,subtype.dtype)
        return data.view(subtype)

    def __array_finalize__(self,obj):
        pass # for now

    def clearGradient(self):
        """Fills all members other than x with zeros."""
        self['dx'][:] = 0.
        self['deltas'][:] = 0.

s = SubRec([3])
s['deltas'][:] = 1.
s['dx'][:] = 2.
print s

s.clearGradient()
print s
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to