[SciPy-user] sequence emulation -/-> array access?

Dan Christensen jdc at uwo.ca
Sat Dec 17 21:14:20 CST 2005


Travis Oliphant <oliphant.travis at ieee.org> writes:

> So, you should be able to subclass rather easily.   

Great, glad to hear it!

> The only confusion, 
> perhaps, is that the array function *is not* a __new__ method.  The 
> ndarray.__new__ method has arguments that are slightly different.  There 
> are basically two ways to create an array (as an empty array or from a 
> buffer).  The docstring of ndarray has more information in newer 
> versions:  help(ndarray).  Also,   look at matrix.py, chararray.py, 
> records.py, and memmap.py for examples of how to subclass.
>
> Basically, you need to define the __new__ method (the only thing that's 
> necessary, no __init__ is needed).   Then define __mul__ and __add__ the 
> way you want.
>
> from scipy import ndarray
>
> class mysub(ndarray):
>     def __new__(self, *args):
>           pass
>           #you need to call ndarray.__new__ in here with one of two sets 
> of arguments

I guess what you are saying is that it's not generally useful to
create an array directly with

  arr = ndarray(...params...)

and so if I subclass without overriding the __new__ method, I won't
be able to conveniently create objects of my new class with

  myob = mysub(...param...)

I'm a bit confused by why ndarray is designed in a way that makes this
necessary, but I should be able to imitate what is done in matrix.py,
etc, to do what I want.  I haven't yet installed scipy core but will
hopefully find time to play with it soon.

Thanks so much for the detailed help!  

Dan



More information about the SciPy-user mailing list