[Numpy-discussion] Preventing an ndarray subclass from returning new subclass instances for std(), etc

Keith Hughitt keith.hughitt at gmail.com
Sun Sep 18 12:09:42 EDT 2011

Hi all,

For a ndarray-subclass I'm working on I extended the array_wrap and
array_finalize functions<https://github.com/sunpy/sunpy/blob/master/sunpy/map/basemap.py#L145-165>
that operations like __sub__() would return a new instance of the subclass,
instead of an ndarray (although I still haven't completely figured this out
yet <http://mail.scipy.org/pipermail/scipy-user/2011-August/030179.html>.)

One side-effect of this, however, is that now other operations which should
*not* return a subclass (such as std) are returning one;

 def __array_finalize__(self, obj):
        """Finishes instantiation of the new map object"""
        if obj is None:

        if hasattr(obj, 'header'):
            self.header = obj.header

            # preserve object properties
            properties = self.get_properties(obj.header)
            for attr, value in list(properties.items()):
                setattr(self, attr, getattr(obj, attr, value))

            self.center = obj.center
            self.scale = obj.scale
            self.units = obj.units

    def __array_wrap__(self, out_arr, context=None):
        """Returns a wrapped instance of a Map object"""
        return np.ndarray.__array_wrap__(self, out_arr, context)

I'm sure it is simply a coding error on my part, but so far I havne't been
able to track it down.

Any ideas?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110918/a53c1033/attachment.html>

More information about the NumPy-Discussion mailing list