473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Subclassing array

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[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 ...

May 4 '06 #1
5 3560
TG <gi****@gmail.com> wrote:
...
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
May 4 '06 #2
Alex Martelli <al*****@yahoo.com> wrote:
TG <gi****@gmail.com> wrote:
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__.


.... so you might want something like:

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

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
May 4 '06 #3
Sion Arrowsmith a écrit :
Alex Martelli <al*****@yahoo.com> wrote:
TG <gi****@gmail.com> wrote:
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__.

... 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')
May 4 '06 #4
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 ?

May 5 '06 #5
TG wrote:
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 !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacek Generowicz | last post by:
The Numeric docs state that "Subclassing Numeric arrays is not possible due to a limitation of Python." What is this limitation? My first guess is that it is the unsbclassability of built-in...
2
by: Gus Tabares | last post by:
Hello all, I'm trying to subclass array.array but having problems with a default parameter in the init constructor. Example: import array class TestClass(array.array): def __init__(self,...
3
by: Mizrandir | last post by:
I'd like to subclass numarray's array. I'd like to add some methods and override others like __init__. I don't know how to do this and haven't found help searching the manual or the web, can...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
2
by: jpecci | last post by:
I would like to ask you a quick question: I am facing the opposite problem described here: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html In short, I want to create a class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.