[Numpy-discussion] suggestion for generalizing numpy functions

Darren Dale dsdale24 at gmail.com
Mon Jul 13 19:12:34 EDT 2009


2009/7/13 Stéfan van der Walt <stefan at sun.ac.za>

> Hi Darren
>
> 2009/7/13 Darren Dale <dsdale24 at gmail.com>:
> > I've put together a first cut at implementing __array_prepare__, which
> > appears to work, and I would like to request feedback. Here is an
> overview
> > of the approach:
>
> This is pretty neat!  Do you have a quick snippet at hand illustrating its
> use?


That would be helpful, wouldn't it? The attached script is a modified
version of RealisticInfoArray from
http://docs.scipy.org/doc/numpy/user/basics.subclassing.html . It should
yield the following output:


starting with [0 1 2 3 4]
which is of type <class '__main__.MyArray'>
and has info attribute = "information"
subtracting 3 from [0 1 2 3 4]
subtract calling __array_prepare__ on [0 1 2 3 4] input
output array is now of type <class '__main__.MyArray'>
output array values are still uninitialized:
        [139911601789568        39578752 139911614885536        39254560
              48]
__array_prepare__ is updating info attribute on output
__array_prepare__ finished, subtract ufunc is taking over
subtract calling __array_wrap__ on [0 1 2 3 4] input
output array has initial value: [-3 -2 -1  0  1]
__array_wrap__ is setting output endpoints to 0
yielding [ 0 -2 -1  0  0]
which is of type <class '__main__.MyArray'>
and has info attribute = "new_information"


Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090713/db9f7522/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: array_prepare.py
Type: text/x-python
Size: 1836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090713/db9f7522/attachment.py>


More information about the NumPy-Discussion mailing list