Native class methods

S

Stefan Arentz

Is there an easy way to implement a specific method of a Python class
in C? Like a native method in Java? I would really like to do the
majority of my class code in Python and just do one or two methods
in C.

S.
 
D

Diez B. Roggisch

Stefan said:
Is there an easy way to implement a specific method of a Python class
in C? Like a native method in Java? I would really like to do the
majority of my class code in Python and just do one or two methods
in C.

ctypes or subclassing C-implemented classes.

Diez
 
C

Chris Mellon

Is there an easy way to implement a specific method of a Python class
in C? Like a native method in Java? I would really like to do the
majority of my class code in Python and just do one or two methods
in C.

S.

Weave kinda does this - you can use it write inline C code, which it
extracts and compiles for you. (http://scipy.org/Weave)

You might also want to look at Pyrex and/or Cython, which let you
write in a Python-like language that is compiled to C.
(http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and
http://cython.org).

Depending on what you want to do in C, just writing it as a normal
shared library and calling it with ctypes might also be an effective
solution. (in the standard library, as of 2.5)
 
S

Stefan Arentz

Chris Mellon said:
Weave kinda does this - you can use it write inline C code, which it
extracts and compiles for you. (http://scipy.org/Weave)

You might also want to look at Pyrex and/or Cython, which let you
write in a Python-like language that is compiled to C.
(http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ and
http://cython.org).

Depending on what you want to do in C, just writing it as a normal
shared library and calling it with ctypes might also be an effective
solution. (in the standard library, as of 2.5)

Yeah I'm really trying to do this without any dependencies on external
libraries. The ctypes way looks interesting but I had really hoped for
something more JNI-like :-/

S.
 
C

Chris Mellon

Yeah I'm really trying to do this without any dependencies on external
libraries. The ctypes way looks interesting but I had really hoped for
something more JNI-like :-/

S.

Weave is a runtime solution, but Pyrex and Cython are both compilers
(they compile a Python like language to C, which you the compile into
a single extension module), and ctypes is in the standard library.
Using ctypes would be pretty much just like JNI, except it's dynamic
and not horrible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top