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

Travis Oliphant oliphant.travis at ieee.org
Sat Dec 17 23:52:31 CST 2005

>>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.
Well, no, you could actually not override the new method and just use 
the same creation function for your subclass.  It's just that the most 
common array creation function "array"0 is *not* the new method.  I 
suppose this is historical more than anything.  Perhaps we should 
rethink the new method of the ndarray so that in fact


has the same behavior as


I'm certainly not opposed to that and have been considering it.  Sooner 
is better than latter, of course. Ideas welcome.


More information about the Scipy-dev mailing list