Subclassing array.array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gus Tabares

    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(arr ay_type)

    [color=blue][color=green][color=darkred]
    >>> temp = TestClass()[/color][/color][/color]
    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
  • Larry Bates

    #2
    Re: Subclassing array.array

    I think you meant to write:
    class TestClass(array .array):
    def __init__(self, array_type = "B"):
    array.array(sel f, 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" <gustabares@ver izon.net> wrote in message
    news:c42b7031.0 406180524.2c5d9 1fc@posting.goo gle.com...[color=blue]
    > 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(arr ay_type)
    >
    >[color=green][color=darkred]
    > >>> temp = TestClass()[/color][/color]
    > 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[/color]


    Comment

    • Peter Otten

      #3
      Re: Subclassing array.array

      Gus Tabares wrote:
      [color=blue]
      > 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(arr ay_type)
      >
      >[color=green][color=darkred]
      >>>> temp = TestClass()[/color][/color]
      > 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.[/color]

      Seems like the argument check happens in __new__(). Try overriding that
      instead:
      [color=blue][color=green][color=darkred]
      >>> class Array(array.arr ay):[/color][/color][/color]
      .... def __new__(cls, tc="B"):
      .... return array.array.__n ew__(cls, tc)
      ....[color=blue][color=green][color=darkred]
      >>> a = Array()
      >>> a[/color][/color][/color]
      array('B')[color=blue][color=green][color=darkred]
      >>> type(a)[/color][/color][/color]
      <class '__main__.Array '>[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Peter

      Comment

      Working...