[Tutor] Python C API - Defining New Classes with Multiple Inheritance

Stefan Behnel stefan_ml at behnel.de
Sat Dec 25 11:49:27 CET 2010


Logan McGrath, 24.12.2010 03:14:
> Hi, I've just started using the Python C API for version 2.7.1, and I've
> got a question!
>
> How do you define a new type which inherits from multiple types?

You can do this for Python classes, but not for C implemented types (which 
are single inheritance by design). Note that you can create both from C 
code, though, so you can just use a Python class if you need multiple 
inheritance and C types for everything else (e.g. for the base classes).


> I've been
> browsing the source code for Python 2.7.1 but I'm having a tough time
> finding examples. I see that MySQLdb defines low-level classes in the
> module "_mysql" using the C API, then extends from them using Python, but I
> want to keep as much of this in C as I can.
>
> Any help would be much appreciated!

The best advice I can give is to use Cython. It's basically a Python 
compiler that generates fast C code for extension modules. So you can just 
write your classes in Python and let Cython compile them for you. That way, 
you get about the same speed as with hand written C code (often faster, 
sometimes slower), but with substantially less coding.

Stefan



More information about the Tutor mailing list