import numpy as np

class TestClass(np.ndarray):

    def __new__(cls, input_array):
        obj = np.array(input_array).view(cls)
        return obj

    def __array_finalize__(self, obj):
        print 'In __array_finalize__:'
        print '   self type %s, values %s' % (type(self), repr(self))
        print '   obj  type %s, values %s' % (type(obj), repr(obj))
        if obj is None: return
        self.doubled_copy = np.array(self) * 2

    def __array_wrap__(self, out_arr, context=None):
        print 'In __array_wrap__:'
        print '   self type %s, values %s' % (type(self), repr(self))
        print '   arr  type %s, values %s' % (type(out_arr), repr(out_arr))
        # then just call the parent
        return np.ndarray.__array_wrap__(self, out_arr, context)

print 'NumPy version: ', np.version.version
print
print 'object creation'
obj = TestClass([0,1])
arr2 = np.array([2,2])
print
print 'object + ndarray'
ret = np.add(arr2, obj)
print
print 'obj= ', obj, obj.doubled_copy, ' ret= ', ret, ret.doubled_copy
