[Numpy-discussion] Subclassing NumArray: _PROTOTYPE flag issue

Todd Miller jmiller at stsci.edu
Mon Jun 21 08:00:05 CDT 2004


On Mon, 2004-06-14 at 06:39, Nadav Horesh wrote:
> For a simulation project I am working on I've subclasses ArrayType. I 
> was able to do much of my intentions until in one place when I tried to 
> make an array from a list of arrays I got an error message:
> 
> .
> .
> .
>   File 
> "/usr/local/lib/python2.3/site-packages/numarray/numarraycore.py", line 
> 325, in array
>     return fromlist(sequence, type, shape)
>   File 
> "/usr/local/lib/python2.3/site-packages/numarray/numarraycore.py", line 
> 212, in fromlist
>     a = a.astype(type)
>   File 
> "/usr/local/lib/python2.3/site-packages/numarray/numarraycore.py", line 
> 630, in astype
>     retarr = self.__class__(buffer=None, shape=self._shape, type=type)
> TypeError: __init__() got an unexpected keyword argument 'buffer'
> 
> The analysis of the code showed that:
> 
>    1. The NumArray class method definitions depends on the _PROTOTYPE flag
>    2. The post-mortem debugging showed that when the error flagged, the
>       value of the variable _PROTOTYPE was 0
> 
> In a stand alone script there was no problem to do the list-> array 
> conversion:
> 
>  >>> import numarray as N
>  >>> import NumImage as NI        # My module with the derived class
>  >>> a = N.arange(4)
>  >>> ia = NI.Cimage(N.arange(4))  # CImage is a derivative of NumImage
>  >>> a
> array([0, 1, 2, 3])
>  >>> ia
> Cimage([0, 1, 2, 3])
>  >>> N.array([a+i for i in range(3)])
> array([[0, 1, 2, 3],
>        [1, 2, 3, 4],
>        [2, 3, 4, 5]])
>  >>> N.array([ia+i for i in range(3)])  # OK here, but failed as a part 
> of a complex script
> Cimage([[0, 1, 2, 3],
>        [1, 2, 3, 4],
>        [2, 3, 4, 5]])
> 
> 
> My questions are:
> 
>    1. Is this flag is in use? 

Yes.

> If I set it to 0 will I be able to derive
>       a class from the "C code"?

Exactly.  _PROTOTYPE hides the original Python implementations of things
that have since been moved to C;  it is a compile time flag.   I keep
the prototype Python code around because it is much easier to debug and
very useful when modifying or extending numarray.

Regards,
Todd





More information about the Numpy-discussion mailing list