Superclass initialization

Arnaud Delobelle arnodel at googlemail.com
Fri Apr 24 16:34:08 CEST 2009


On Apr 24, 3:04 pm, Ole Streicher <ole-usenet-s... at gmx.net> wrote:
> Hi again,
>
> I am trying to initialize a class inherited from numpy.ndarray:
>
> from numpy import ndarray
>
> class da(ndarray):
>     def __init__(self, mydata):
>         ndarray.__init__(self, 0)
>         self.mydata = mydata
>
> When I now call the constructor of da:
> da(range(100))
>
> I get the message:
>
> ValueError: sequence too large; must be smaller than 32
>
> which I do not understand. This message is generated by the
> constructor of ndarray, but the ndarray constructor
> (ndarray.__init__()) has only "0" as argument, and calling
> "ndarray(0)" directly works perfect.
>
> In the manual I found that the constructor of a superclass is not
> called implicitely, so there should be no other call to
> ndarray.__init__() the the one in my __init__ method.
>
> I am now confused on where does the call to ndarray come from. How do
> I correct that?
>
> Best regards
>
> Ole

numpy.ndarray has a __new__ method (and no __init__).  I guess this is
the one you should override.  Try:

class da(ndarray):
    def __new__(cls, mydata):
        return ndarray.__new__(cls, 0)
    def __init__(self, mydata):
        self.mydata = mydata

--
Arnaud





More information about the Python-list mailing list