Problems with subclassing

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

    #1

    Problems with subclassing

    Hello,

    I have the following problem. I want to subclass from the pygame class
    Surface, but I can't get it working:

    class sprite(Surface) :
    def __init__(self, image_array, palette):
    "some code here"

    when I want to create a instance with:

    gfx.sprite(arra yi, palette)


    I get the following error message:
    TypeError: argument 1 must be sequence of length 2, not 100

    It seems that the constructor of Surface is not overrided by the one in
    sprite, but why?

  • Alex Martelli

    #2
    Re: Problems with subclassing

    Alexander Stante <alex_stante@ya hoo.de> wrote:
    [color=blue]
    > I have the following problem. I want to subclass from the pygame class
    > Surface, but I can't get it working:
    >
    > class sprite(Surface) :
    > def __init__(self, image_array, palette):
    > "some code here"
    >
    > when I want to create a instance with:
    >
    > gfx.sprite(arra yi, palette)
    >
    >
    > I get the following error message:
    > TypeError: argument 1 must be sequence of length 2, not 100
    >
    > It seems that the constructor of Surface is not overrided by the one in
    > sprite, but why?[/color]

    It's possible (I'm just guessing) that the problem comes with respect to
    __new__ rather than __init__ ...?


    Alex

    Comment

    • Gerrit

      #3
      Re: Problems with subclassing

      Alexander Stante wrote:[color=blue]
      > I have the following problem. I want to subclass from the pygame class
      > Surface, but I can't get it working:
      >
      > class sprite(Surface) :
      > def __init__(self, image_array, palette):
      > "some code here"
      >
      > when I want to create a instance with:
      >
      > gfx.sprite(arra yi, palette)
      >
      >
      > I get the following error message:
      > TypeError: argument 1 must be sequence of length 2, not 100
      >
      > It seems that the constructor of Surface is not overrided by the one in
      > sprite, but why?[/color]

      I think you want to override __new__, not __init__, if if could be
      working at all that is. GvR wrote a lengthy introduction to this (among
      other things) when Py 2.2 was released: google for "descriptor " and it
      should end up somewhere.

      It's also possible that Surface is not a viable baseclass at all. You
      may want to consult the Pygame mailing list about that.

      yours,
      Gerrit.

      --
      Weather in Twenthe, Netherlands 13/09 08:25:
      14.0°C Broken clouds mostly cloudy wind 4.5 m/s S (57 m above NAP)
      --
      Gerrit Holl - 2nd year student of Applied Physics, Twente University, NL.
      Experiences with Asperger's Syndrome:
      EN http://topjaklont.student.utwente.nl/english/
      NL http://topjaklont.student.utwente.nl/

      Comment

      • Chris S.

        #4
        Re: Problems with subclassing

        Alexander Stante wrote:
        [color=blue]
        > Hello,
        >
        > I have the following problem. I want to subclass from the pygame class
        > Surface, but I can't get it working:
        >
        > class sprite(Surface) :
        > def __init__(self, image_array, palette):
        > "some code here"
        >
        > when I want to create a instance with:
        >
        > gfx.sprite(arra yi, palette)
        >
        >
        > I get the following error message:
        > TypeError: argument 1 must be sequence of length 2, not 100
        >
        > It seems that the constructor of Surface is not overrided by the one in
        > sprite, but why?
        >[/color]

        Try manually specifying the subclassed constructor within your new
        constructor. For example:

        class sprite(Surface) :
        def __init__(self, image_array, palette):
        Surface.__init_ _(self, someargs)

        Then, ensure you are instantiating the subclass correctly.

        Comment

        Working...