[SciPy-User] subclassing ndarray : i want slice to return ndarray, not subclass

pierre puiseux UPPA pierre.puiseux@univ-pau...
Mon May 21 04:56:10 CDT 2012


Hello,
all my question is in the title. More precisely, like in scipy doc i try this :
======================================
import sys
import numpy as np

class ArrayChild(np.ndarray):

    def __new__(cls, array, info=None):
        obj = np.asarray(array).view(cls)
        obj.info = info
        return obj

    def __array_finalize__(self, obj):
        print>>sys.stderr, "__array_finalize__"
        if obj is None: return
        self.info = getattr(obj, 'info', None)

if __name__=='__main__':
    a = np.arange(6)
    a.shape=2,3
    a_child = ArrayChild(a)
    for x in a_child : 
        print>>sys.stderr, x, type(x)

=====================================
and i have the answer :
=====================================
__array_finalize__
__array_finalize__
[0 1 2] <class '__main__.ArrayChild'>
__array_finalize__
[3 4 5] <class '__main__.ArrayChild'>

=====================================
but i want
=====================================
__array_finalize__
__array_finalize__
[0 1 2] <class '__main__.np.ndarray'>
__array_finalize__
[3 4 5] <class '__main__.np.ndarray'>

=====================================
I've tried to redefine : __getslice__  like this, but it does not work.
=====================================
    def __getslice__(self, *args, **kwargs):
        return np.ndarray.__getslice__(self, *args, **kwargs)


=====================================
Some idea ?
Thanks to numpy/scipy team for that great job.
=====================================

Pierre Puiseux
Laboratoire de Mathématiques Appliquées
Université de Pau et des Pays de l'Adour
pierre.puiseux@univ-pau.fr
http://www.univ-pau.fr/~puiseux

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120521/f9c4d981/attachment-0001.html 


More information about the SciPy-User mailing list