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


Groups > comp.lang.python > #98948 > unrolled thread

Writing SOME class methods in C

Started byDaniel Haude <dh@dotcom.mfs32>
First post2015-11-18 07:50 +0000
Last post2015-11-29 14:26 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#98948 — Writing SOME class methods in C

FromDaniel Haude <dh@dotcom.mfs32>
Date2015-11-18 07:50 +0000
SubjectWriting SOME class methods in C
Message-ID<slrnn4obfa.db7.dh@dotcom.mfs32>
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, nor modules that define classes of which some members are
implemented in C, others in Python. Of course, once I have the "mixture" bit
figured out I could just define wrapper class methods that call C functions
(functions impleneted in C, that is). But I would find it rather elegant if
the C function could access the class members directly. The fact that the
C extension functions have a mandatory "PyObject *self" parameter tells me
that this must be somehow possible, but I don't know how.

I'm sure that many of the plethora of Python extension modules out there
must use the technique that I'm looking for, but I don't even know where to
start looking. Pointers are welcome.

Thanks,
robert

[toc] | [next] | [standalone]


#98954

FromTerry Reedy <tjreedy@udel.edu>
Date2015-11-18 04:47 -0500
Message-ID<mailman.407.1447840093.16136.python-list@python.org>
In reply to#98948
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

[toc] | [prev] | [next] | [standalone]


#98964

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2015-11-18 12:52 +0000
Message-ID<mailman.412.1447851580.16136.python-list@python.org>
In reply to#98948
On 18 November 2015 at 07:50, Daniel Haude <dh@dotcom.mfs32> 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.

Alternatively Terry's suggestion to implement a class in C and then
subclass it in Python to add the remaining methods is a good one. But
then I would write the C class using Cython so I may as well do the
whole thing in Cython.

--
Oscar

[toc] | [prev] | [next] | [standalone]


#99693

FromStefan Behnel <stefan_ml@behnel.de>
Date2015-11-29 14:26 +0100
Message-ID<mailman.8.1448803612.14615.python-list@python.org>
In reply to#98948
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web