Subclassing array.array

G

Gus Tabares

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)

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
 
L

Larry Bates

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

Peter Otten

Gus said:
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)


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:
.... def __new__(cls, tc="B"):
.... return array.array.__new__(cls, tc)
....
Peter
 

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
474,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top