By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

Subclassing array.array

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.