Superclass initialization

O

Ole Streicher

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
 
S

Steven D'Aprano

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.


Perhaps you should post the full trace back instead of just the final
line.
 
A

Arnaud Delobelle

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
 
O

Ole Streicher

Steven D'Aprano said:
Perhaps you should post the full trace back instead of just the final
line.

No Problem, although I dont see the information increase there:

In [318]: class da(ndarray):
.....: def __init__(self, mydata):
.....: ndarray.__init__(self, 0)
.....: self.mydata = mydata
.....:

In [319]: da(range(100))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/m3d/src/python/<ipython console> in <module>()

ValueError: sequence too large; must be smaller than 32

The same happens if I put the class definition into a file: the
traceback does *not* point to a code line in that source file but to
the input line. Again, full trace:

In [320]: import da

In [321]: da.da(range(100))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/m3d/src/python/<ipython console> in <module>()

ValueError: sequence too large; must be smaller than 32

(using python instead of ipython also does not give more details).

Best regards

Ole
 
O

Ole Streicher

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

What is the difference?

best regards

Ole
 
A

Aahz

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.

Did you read the docs? According to
http://docs.scipy.org/numpy/docs/numpy.ndarray/#numpy-ndarray
the first argument to an ndarray constructor is the shape, and I suppose
arrays might be limited to 32 dimensions. Anyway, I suggest that you
subscribe to one of the NumPy mailing lists and ask there:
http://scipy.org/Mailing_Lists
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top