[Numpy-discussion] subclassing ndaray

Travis Oliphant oliphant.travis at ieee.org
Fri Feb 24 17:41:14 CST 2006


Stefan van der Walt wrote:

>I see the same strange result.  Here is a minimal code example to
>demonstrate:
>
>import numpy as N
>
>class Bar(N.ndarray):
>    v = 0.
>    
>    def __new__(cls, *args, **kwargs):
>	print "running new"
>	return super(Bar, cls).__new__(cls, *args)
>
>    def __init__(self, *args, **kwargs):
>	print "running init"
>	self[:] = 0
>	self.v = 3
>  
>
It's only strange if you have assumptions your not revealing.  Here's 
the deal.

Neither the __init__ method nor the __new__ method are called for c = b+1.

So, your wondering how the Bar object got created then right?  Well, it 
got created as a subclass of ndarray in PyArray_NewFromDescr. 

The __init__ and __new__ methods are not called because they may have 
arbitrary signatures.  Instead,  the __array_finalize__ method is always 
called.  So, you should use that instead of __init__.

The __array_finalize__ method always receives the argument of the 
"parent" object.

Thus in your case.

       def __array_finalize__(self, parent):
             self.v = 3

would do what you want.


-Travis




>In [88]: b = Bar(3)
>running new
>running init
>
>In [89]: b
>Out[89]: Bar([0, 0, 0])
>
>In [90]: b.v
>Out[90]: 3
>
>In [91]: c = b+1
>
>In [92]: c.v
>Out[92]: 0.0
>
>However, if I do b[:] = 1, everything works fine.
>
>Stéfan
>
>On Fri, Feb 24, 2006 at 10:56:02AM -0500, Colin J. Williams wrote:
>  
>
>>I have a subclass Bar, a 1-dim array which has some methods and some 
>>attributes.  One of the attributes is a view of the Bar to permit 
>>different shaping.
>>
>>Suppose that 'a' is an instance of 'Bar', which has a method 'show' and 
>>a view attribute 'v'. 
>>
>>a ^ 15 returns a Bar instance, with its methods but without the attributes.
>>
>>I am attempt to change this, Bar has a method __xor__, see below:
>>
>>     def __xor__(self, other):
>>       ''' Exclusive or: __xor__(x, y) => x ^ y . '''
>>       z=
>>   1                                                                    
>>   <<  this loops to the recursion limit
>>       result= ArrayType.__xor__(self, other)
>>       n= self.n
>>       result.n= n
>>       result.rowSize= self.rowSize
>>       result.show= self.show
>>       result.v= _n.reshape(result.view(), (n*n, n*n))
>>       return result
>>
>>Could anyone suggest a workaround please?
>>
>>Colin W.
>>    
>>
>
>
>-------------------------------------------------------
>This SF.Net email is sponsored by xPML, a groundbreaking scripting language
>that extends applications into web and mobile media. Attend the live webcast
>and join the prime developer group breaking into this new coding territory!
>http://sel.as-us.falkag.net/sel?cmd=k&kid0944&bid$1720&dat1642
>_______________________________________________
>Numpy-discussion mailing list
>Numpy-discussion at lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>  
>





More information about the Numpy-discussion mailing list