Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Subclassing array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="TG, post: 1878169"] 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 :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 ...[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Subclassing array
Top