471,318 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Native class methods


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.

Oct 9 '07 #1
4 1513
Stefan Arentz wrote:
>
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
Oct 9 '07 #2
On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <st***********@gmail.comwrote:
>
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/gre.../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)
Oct 9 '07 #3
"Chris Mellon" <ar*****@gmail.comwrites:
On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <st***********@gmail.comwrote:

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/gre.../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.

Oct 9 '07 #4
On 09 Oct 2007 17:45:12 +0200, Stefan Arentz <st***********@gmail.comwrote:
"Chris Mellon" <ar*****@gmail.comwrites:
On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <st***********@gmail.comwrote:
>
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/gre.../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.
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.
Oct 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Herby | last post: by
1 post views Thread by =?Utf-8?B?RmFiaWFu?= | last post: by
2 posts views Thread by Andreas Schmitt | last post: by
6 posts views Thread by =?Utf-8?B?RmFiaWFu?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.