Subclassing array.array
Peter Otten
__peter__ at web.de
Fri Jun 18 12:38:01 EDT 2004
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
More information about the Python-list
mailing list