Subclassing array

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

    #1

    Subclassing array

    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 :

    class Vector(array):
    def __new__(cls,len gth,data=None):
    return super(Vector,cl s).__new__(cls, 'f')

    def __init__(self,l ength,data=None ):
    if data == None:
    for _ in xrange(length):
    self.append(0.0 )
    else:
    for i in xrange(length):
    self.append(dat a[i])



    Now, i want to inherit from this vector class :

    class Stimulus(Vector ):
    def __init__(self,w idth,height,lab el,data=None):
    Vector.__init__ (self,width*hei ght,data)
    self.width = width
    self.height = height
    self.label = label

    This doesn't seem to work :[color=blue][color=green][color=darkred]
    >>> s = Stimulus(10,10, "data")[/color][/color][/color]
    TypeError: __new__() takes at most 3 arguments (4 given)

    In order to make it work, it seems that I have to redefine __new__
    again, like this.

    def __new__(cls,wid th,height,label ,data=None):
    return super(Stimulus, cls).__new__(cl s,width*height)

    Why is that ?
    When I call Vector.__init__ () in Stimulus, doesn't it also call __new__
    ? I don't understand the detail of callings to __new__ and __init__ in
    python inheritance ...

  • Alex Martelli

    #2
    Re: Subclassing array

    TG <girodt@gmail.c om> wrote:
    ...[color=blue]
    > When I call Vector.__init__ () in Stimulus, doesn't it also call __new__
    > ? I don't understand the detail of callings to __new__ and __init__ in
    > python inheritance ...[/color]

    Calling a (new-style) class does __new__ first, THEN calls the class's
    __init__ on the resulting instance -- and the arguments you're passing
    when calling the class go to both __new__ and __init__.


    Alex

    Comment

    • Sion Arrowsmith

      #3
      Re: Subclassing array

      Alex Martelli <aleaxit@yahoo. com> wrote:[color=blue]
      >TG <girodt@gmail.c om> wrote:[color=green]
      >> When I call Vector.__init__ () in Stimulus, doesn't it also call __new__
      >> ? I don't understand the detail of callings to __new__ and __init__ in
      >> python inheritance ...[/color]
      >Calling a (new-style) class does __new__ first, THEN calls the class's
      >__init__ on the resulting instance -- and the arguments you're passing
      >when calling the class go to both __new__ and __init__.[/color]

      .... so you might want something like:

      class Vector(array):
      def __new__(cls,*ar gs):
      return super(Vector,cl s).__new__(cls, 'f')

      --
      \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
      ___ | "Frankly I have no feelings towards penguins one way or the other"
      \X/ | -- Arthur C. Clarke
      her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

      Comment

      • Bruno Desthuilliers

        #4
        Re: Subclassing array

        Sion Arrowsmith a écrit :[color=blue]
        > Alex Martelli <aleaxit@yahoo. com> wrote:
        >[color=green]
        >>TG <girodt@gmail.c om> wrote:
        >>[color=darkred]
        >>>When I call Vector.__init__ () in Stimulus, doesn't it also call __new__
        >>>? I don't understand the detail of callings to __new__ and __init__ in
        >>>python inheritance ...[/color]
        >>
        >>Calling a (new-style) class does __new__ first, THEN calls the class's
        >>__init__ on the resulting instance -- and the arguments you're passing
        >>when calling the class go to both __new__ and __init__.[/color]
        >
        >
        > ... so you might want something like:
        >
        > class Vector(array):
        > def __new__(cls,*ar gs):
        > return super(Vector,cl s).__new__(cls, 'f')
        >[/color]
        And if you want to support named arguments:

        class Vector(array):
        def __new__(cls,*ar gs, **kw):
        return super(Vector,cl s).__new__(cls, 'f')

        Comment

        • TG

          #5
          Re: Subclassing array

          That's great, thanks !

          To put it short, when I create a Stimulus object, it first seek
          __new__() method. But if I don't define it, it looks for the one
          defined in Vector. This raises a problem because the parameters passed
          to Stimulus(params ) aren't fitting with Vector parameters, raising an
          exception.

          That's why I have to use this *arg **kw syntax in order to allow my
          subclasses having any arguments without causing troubles. Am I right ?

          Comment

          • bruno at modulix

            #6
            Re: Subclassing array

            TG wrote:[color=blue]
            > That's great, thanks !
            >
            > To put it short, when I create a Stimulus object, it first seek
            > __new__() method. But if I don't define it, it looks for the one
            > defined in Vector. This raises a problem because the parameters passed
            > to Stimulus(params ) aren't fitting with Vector parameters, raising an
            > exception.
            >
            > That's why I have to use this *arg **kw syntax in order to allow my
            > subclasses having any arguments without causing troubles. Am I right ?
            >[/color]

            To put it short : yes !-)

            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            Working...