[Numpy-discussion] __array_wrap__ / __array_finalize__ in NumPy v1.4+

Aronne Merrelli aronne.merrelli@gmail....
Sun Aug 14 18:51:10 CDT 2011


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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110814/6227d209/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: array_wrap_test.py
Type: application/octet-stream
Size: 1032 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110814/6227d209/attachment-0001.obj 


More information about the NumPy-Discussion mailing list