[Numpy-discussion] subclassing float64 (and friends)

Travis Oliphant oliphant at ee.byu.edu
Wed Jan 3 15:53:43 CST 2007


eric jones wrote:

>Hey all,
>
>I am playing around with sub-classing the new-fangled float64 objects 
>and friends.  I really like the new ndarray subclassing features 
>(__array_finalize__, etc.), and was exploring whether or not the scalars 
>worked the same way.  I've stubbed my toe right out of the blocks 
>though.  I can sub-class from standard python floats just fine, but when 
>I try to do the same from float64, I get a traceback. (examples below)  
>Anyone have ideas on how to do this correctly?
>  
>

Unfortunately, the array scalars were not originally designed to be 
sub-classed.   I believe I later set the flag saying they "could be" 
sub-classable.  However, I did not alter the tp_new code to actually use 
the type information passed in as the first argument to actually create 
an instance of the sub-class --- effectively making the array scalars 
*not* sub-classable.

Thus, the _arrtype_new code would need to be changed in 
scalartypes.inc.src to fix this.   Notice that in Python the float_new 
command does a special check for when a sub-type is requested and calls 
float_subtype_new which creates a regular float and then copies the data 
over to the sub-type.  

This special-check is not done in the tp_new function 
(<name>_arrtype_new) and so float64.__new__ will *always* return the 
array scalars are still not sub-classable (except for the void 
scalar).   This could be changed, and needs to happen before 
sub-classing of array scalars will work.

-Travis





More information about the Numpy-discussion mailing list