[Numpy-discussion] basearray / arraykit

Sasha ndarray at mac.com
Wed May 10 14:05:04 EDT 2006


On 5/8/06, Tim Hochberg <tim.hochberg at cox.net> wrote:
> [...] Here's a brief example;
> this is what an custom array class that just supported indexing and
> shape would look like using arraykit:
>
>     import numpy.arraykit as _kit
>
>     class customarray(_kit.basearray):
>         __new__ = _kit.fromobj
>         __getitem__ = _kit.getitem
>         __setitem__ = _kit.setitem
>         shape = property(_kit.getshape, _kit.setshape)

I see the following problem with your approach: customarray.__new__ is
supposed to return an instance of customarray, but in your example it
returns a basearray.  You may like an approach that I took in writing
r.py <https://svn.sourceforge.net/svnroot/rpy/trunk/sandbox/r.py>.  In
the context of your example, I would make fromobj a classmethod of
_kit.basearray and use the type argument to allocate the new object
(type->tp_alloc(type, 0);).   This way customarray(...) will return
customarray as expected.

All _kit methods that return arrays can take the same approach and
become classmethods of _kit.basearray.  The drawback is the pollution
of the base class namespace, but this may be acceptable if you name
the baseclass methods with a leading underscore.




More information about the NumPy-Discussion mailing list