[Numpy-discussion] Subclassing and metadata

Travis Oliphant oliphant at ee.byu.edu
Tue Apr 12 16:39:23 CDT 2005


I think I've found a possible solution for subclasses that want to 
handle metadata.

Essentially, any subclass that defines the method _update_meta(self, other)
will get that method called when an array is sliced, or subscripted.

Anytime an array is created where a subtype is the caller, this method 
will be called if it is available.


Here is a simple example:

import ndarray

class subclass(ndarray.ndarray):
    def __new__(self, shape, *args, **kwds):
        self = ndarray.ndarray.__new__(subclass, shape, 'V4')
        return self

    def __init__(self, shape, *args, **kwds):
        self.dict = kwds
        return
   
    def _update_meta(self, obj):
        self.dict = obj.dict


Comments?

-Travis





More information about the Numpy-discussion mailing list