Problems with subclassing

A

Alexander Stante

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(arrayi, 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?
 
A

Alex Martelli

Alexander Stante said:
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(arrayi, 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?

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


Alex
 
G

Gerrit

Alexander said:
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(arrayi, 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?

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

Chris S.

Alexander said:
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(arrayi, 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?

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.
 

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,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top