on-internet
← Community

How to subclass Python types inside C extensions?

Stack Overflow ·

I know how to define a new type (class) in a Python extension, but is it also possible to define a class that is derived from a Python class? For example, in Python I could define an "extended" list class like so: class xlist(list): def xsum(self): return sum(self) How would I derive the xlist type from PyList inside a C extension? I wanted to try it out by declaring a PyTypeObject variable and memcpy()ing the contents of the PyList_Type instance into it, but I couldn't figure out how to add the xsum method to the new type.

Comments

Sign in or sign up to comment and upvote.

No comments yet.