Skip to content Skip to sidebar Skip to footer

Python: Unable To Inherit From A C Extension

I am trying to add a few extra methods to a matrix type from the pysparse library. Apart from that I want the new class to behave exactly like the original, so I chose to implement

Solution 1:

ll_mat is documented to be a function -- not the type itself. The idiom is known as "factory function" -- it allows a "creator callable" to return different actual underlying types depending on its arguments.

You could try to generate an object from this and then inherit from that object's type:

x = spmatrix.ll_mat(10, 10)
classll_mat(type(x)): ...

be aware, though, that it's quite feasible for a built-in type to declare it won't support being subclassed (this could be done even just to save some modest overhead); if that's what that type does, then you can't subclass it, and will rather have to use containment and delegation, i.e.:

classll_mat(object):def__init__(self, *a, **k):
        self.m = spmatrix.ll_mat(*a, **k)
        ...
    def__getattr__(self, n):
        return getattr(self.m, n)

etc, etc.

Post a Comment for "Python: Unable To Inherit From A C Extension"