Writing SOME class methods in C
Terry Reedy
tjreedy at udel.edu
Wed Nov 18 04:47:56 EST 2015
On 11/18/2015 2:50 AM, Daniel Haude wrote:
> Hello,
>
> 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
>
> But I can't find any examples of modules which consist of a mixture of C and
> Python,
There at least to be such in the stdlib. The .py module defined
*everything* and then ended with
try:
from _module import *
except ImportError:
pass
to replace whatever top-level objects were also written in C. The
try-except part is optional but let the module run when _module was not
present. I believe the string module was once like this.
> nor modules that define classes of which some members are
> implemented in C, others in Python.
I would try putting the C part in separate base or mix-in class,
imported before the class statement. To make the C part optional:
class mixin: ...
try:
from _module import mixin
except ImportError
pass
class myclass(mixin): ...
--
Terry Jan Reedy
More information about the Python-list
mailing list