Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98954

Re: Writing SOME class methods in C

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Writing SOME class methods in C
Date 2015-11-18 04:47 -0500
Message-ID <mailman.407.1447840093.16136.python-list@python.org> (permalink)
References <slrnn4obfa.db7.dh@dotcom.mfs32>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Writing SOME class methods in C Daniel Haude <dh@dotcom.mfs32> - 2015-11-18 07:50 +0000
  Re: Writing SOME class methods in C Terry Reedy <tjreedy@udel.edu> - 2015-11-18 04:47 -0500
  Re: Writing SOME class methods in C Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-18 12:52 +0000
  Re: Writing SOME class methods in C Stefan Behnel <stefan_ml@behnel.de> - 2015-11-29 14:26 +0100

csiph-web