How to change the superclass of an instance?

Alexander Stante alex_stante at yahoo.de
Mon Sep 13 17:20:59 EDT 2004


Hello,

I want to do the following. I have a function called display.set_mode()
which returns an instance of Surface. I want to add some bound
functions to this object therefore I thought to do something like:

>>> class screen(Surface):
...     def __new__(cls):
...         a = display.set_mode()
...         a.__class__ = screen
...         return a
...     def new_func(self):
...         pass

My idea was that my new created object has an attribute resolution
order of instance -> screen -> Surface and therefore I can use all
Surface methods on the instance and the new_func(). But I got the error
message: "TypeError: __class__ assignment: only for heap types"

I also tried to add a bound function (to the returned Surface of
display.set_mode()) with setattr and the appropriate arguments but I
got the error message: pygame.Surface object has no attribute 'update'
so I think Surface is somehow implemented immutable by intercepting
attribute access via __getattribute__

My question, how can I realize that I described above, because my
attempt seems to be wrong.

An other idea I had would be wrapping a class around the Surface
returned by display.set_mode() and then intercepting attribute
references with __getattr__ on that class and if not found in class or
instance, delegate them to the Surface. I think this way it would look
liked the wrapping class is a subclass of Surface. But Surface don't
have a __dict__ and I don't know an other way to call a function by his
name.

Bye
Alex




More information about the Python-list mailing list