Writing SOME class methods in C

Stefan Behnel stefan_ml at behnel.de
Sun Nov 29 08:26:41 EST 2015


Oscar Benjamin schrieb am 18.11.2015 um 13:52:
> On 18 November 2015 at 07:50, Daniel Haude wrote:
>>
>> I'm trying to implement some (but not all) methods of a Python class in C.
>> What I've found on the Net is:
>>  - how to implement entire modules in C so that I can import that module and
>>    use the C functions (successfully done it, too).
>>  - how to implement entire classes in C
> 
> I would suggest to use Cython here. You can write your class in Python
> (that will be compiled to C) and then call out to any C code from any
> of its methods.

Or, in fact, do the reverse: Implement the base class in Cython and inherit
from it in a Python class that extends it. That would give you a fast,
native extension type at the base and leaves you with all the freedom to
extend it in Python code or even natively in other Cython code.

I strongly recommend not to resort to writing real C code here (using the
C-API of CPython). It will be slower and will contain more bugs.

Stefan





More information about the Python-list mailing list