Subclassing array
TG
girodt at gmail.com
Thu May 4 08:51:36 EDT 2006
Hi.
i've already something about inheriting from array a few weeks ago and
had my answer. But again, there is something that I don't understand.
Here is my vector class, which works quite well :
class Vector(array):
def __new__(cls,length,data=None):
return super(Vector,cls).__new__(cls,'f')
def __init__(self,length,data=None):
if data == None:
for _ in xrange(length):
self.append(0.0)
else:
for i in xrange(length):
self.append(data[i])
Now, i want to inherit from this vector class :
class Stimulus(Vector):
def __init__(self,width,height,label,data=None):
Vector.__init__(self,width*height,data)
self.width = width
self.height = height
self.label = label
This doesn't seem to work :
>>> s = Stimulus(10,10,"data")
TypeError: __new__() takes at most 3 arguments (4 given)
In order to make it work, it seems that I have to redefine __new__
again, like this.
def __new__(cls,width,height,label,data=None):
return super(Stimulus,cls).__new__(cls,width*height)
Why is that ?
When I call Vector.__init__() in Stimulus, doesn't it also call __new__
? I don't understand the detail of callings to __new__ and __init__ in
python inheritance ...
More information about the Python-list
mailing list