473,386 Members | 1,793 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,386 software developers and data experts.

Subclassing array.array

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, array_type = "B"):
array.array(array_type)

temp = TestClass()

Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
temp = TestClass()
TypeError: array() takes at least 1 argument (0 given)

I think there is something that I'm not understanding here. Any help
is appreciated.
Thanks,
Gus
Jul 18 '05 #1
2 2033
I think you meant to write:
class TestClass(array.array):
def __init__(self, array_type = "B"):
array.array(self, array_type)

Note that your "default" got gobbled upwhen
array.array was looking for first argument
to be self.

HTH,
Larry Bates
Syscon, Inc.
"Gus Tabares" <gu********@verizon.net> wrote in message
news:c4**************************@posting.google.c om...
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, array_type = "B"):
array.array(array_type)

temp = TestClass()

Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
temp = TestClass()
TypeError: array() takes at least 1 argument (0 given)

I think there is something that I'm not understanding here. Any help
is appreciated.
Thanks,
Gus

Jul 18 '05 #2
Gus Tabares wrote:
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, array_type = "B"):
array.array(array_type)

temp = TestClass() Traceback (most recent call last):
File "<pyshell#1>", line 1, in ?
temp = TestClass()
TypeError: array() takes at least 1 argument (0 given)

I think there is something that I'm not understanding here. Any help
is appreciated.


Seems like the argument check happens in __new__(). Try overriding that
instead:
class Array(array.array): .... def __new__(cls, tc="B"):
.... return array.array.__new__(cls, tc)
.... a = Array()
a array('B') type(a) <class '__main__.Array'>


Peter

Jul 18 '05 #3

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...
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...
5
by: TG | last post by:
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 :...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.