Subclassing array

T

TG

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)



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 :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 ...
 
A

Alex Martelli

TG said:
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 ...

Calling a (new-style) class does __new__ first, THEN calls the class's
__init__ on the resulting instance -- and the arguments you're passing
when calling the class go to both __new__ and __init__.


Alex
 
S

Sion Arrowsmith

Alex Martelli said:
Calling a (new-style) class does __new__ first, THEN calls the class's
__init__ on the resulting instance -- and the arguments you're passing
when calling the class go to both __new__ and __init__.

.... so you might want something like:

class Vector(array):
def __new__(cls,*args):
return super(Vector,cls).__new__(cls,'f')
 
B

Bruno Desthuilliers

Sion Arrowsmith a écrit :
... so you might want something like:

class Vector(array):
def __new__(cls,*args):
return super(Vector,cls).__new__(cls,'f')
And if you want to support named arguments:

class Vector(array):
def __new__(cls,*args, **kw):
return super(Vector,cls).__new__(cls,'f')
 
T

TG

That's great, thanks !

To put it short, when I create a Stimulus object, it first seek
__new__() method. But if I don't define it, it looks for the one
defined in Vector. This raises a problem because the parameters passed
to Stimulus(params) aren't fitting with Vector parameters, raising an
exception.

That's why I have to use this *arg **kw syntax in order to allow my
subclasses having any arguments without causing troubles. Am I right ?
 
B

bruno at modulix

TG said:
That's great, thanks !

To put it short, when I create a Stimulus object, it first seek
__new__() method. But if I don't define it, it looks for the one
defined in Vector. This raises a problem because the parameters passed
to Stimulus(params) aren't fitting with Vector parameters, raising an
exception.

That's why I have to use this *arg **kw syntax in order to allow my
subclasses having any arguments without causing troubles. Am I right ?

To put it short : yes !-)
 

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,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top