I'm trying to extend functionality of a module class written in C by deriving from that class and overwriting / adding certain methods*.
Problem is that his module creates instances of the class I'm trying to extend at various different places. I thus need to create a casting method to cast the base class provided by the module to my own derived class with the additonal functionality.
Here's what I've tried:
class Derived(Base):
@classmethod
def cast(cls, obj: Base):
obj.__class__ = cls
This method works with class hierarchies that I've created myself - however it fails with the module I use, throwing the following exception:
TypeError: __class__ assignment only supported for heap types or ModuleType subclasses
I'm having a hard time finding official information about this exception. Any info helps, I'd even accept hacky solutions so long as they're clean and fully mimic the behavior I'm looking for.
* The class I'm trying to extend is Surface
inside the package pygame
.