Hello, I'm attempting to implement a subclass of ndarray, and becoming confused about the way __array_wrap__ and __array_finalize__ operate. I boiled it down to a short subclass, which is the example on the website at http://docs.scipy.org/doc/numpy-1.6.0/user/basics.subclassing.html, with one added attribute that is a copy of the self array multiplied by 2. The doubled copy is stored in a "plain" ndarray. The attachment has the python code.
The output below is from NumPy 1.3 and 1.6 (1.4 has the same output as 1.6). The output from 1.3 matches the documentation on the website. In 1.6, __array_wrap__ and __array_finalize__ are invoked in the opposite order, __array_finalize__ appears to be getting an "empty" array, and array_wrap's argument is no longer an ndarray but rather an instance of the subclass. This doesn't match the documentation so I am not sure if this is the correct behavior in newer NumPy. Is this a bug, or the expected behavior in newer NumPy versions? Am I just missing something simple? The actual code I am trying to write uses essentially the same idea - keeping another array, related to the self array through some calculation, as another object attribute. Is there a better way to accomplish this? Thanks, Aronne NumPy version: 1.3.0 object creation In __array_finalize__: self type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) obj type <type 'numpy.ndarray'>, values array([0, 1]) object + ndarray In __array_wrap__: self type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) arr type <type 'numpy.ndarray'>, values array([2, 3]) In __array_finalize__: self type <class 'array_wrap_test.TestClass'>, values TestClass([2, 3]) obj type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) obj= [0 1] [0 2] ret= [2 3] [4 6] NumPy version: 1.6.0 object creation In __array_finalize__: self type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) obj type <type 'numpy.ndarray'>, values array([0, 1]) object + ndarray In __array_finalize__: self type <class 'array_wrap_test.TestClass'>, values TestClass([ 15, 22033837]) obj type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) In __array_wrap__: self type <class 'array_wrap_test.TestClass'>, values TestClass([0, 1]) arr type <class 'array_wrap_test.TestClass'>, values TestClass([2, 3]) obj= [0 1] [0 2] ret= [2 3] [ 30 44067674]
array_wrap_test.py
Description: Binary data
_______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
