Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Native class methods

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


stefan.arentz at gmail

Oct 9, 2007, 8:20 AM

Post #1 of 6 (442 views)
Permalink
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.

--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Oct 9, 2007, 8:25 AM

Post #2 of 6 (421 views)
Permalink
Re: Native class methods [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


arkanes at gmail

Oct 9, 2007, 8:28 AM

Post #3 of 6 (417 views)
Permalink
Re: Native class methods [In reply to]

On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <stefan.arentz [at] gmail> 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.
>
> 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)
--
http://mail.python.org/mailman/listinfo/python-list


stefan.arentz at gmail

Oct 9, 2007, 8:45 AM

Post #4 of 6 (417 views)
Permalink
Re: Native class methods [In reply to]

"Chris Mellon" <arkanes [at] gmail> writes:

> On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <stefan.arentz [at] gmail> 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.
> >
> > 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)

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.

--
http://mail.python.org/mailman/listinfo/python-list


exarkun at divmod

Oct 9, 2007, 9:06 AM

Post #5 of 6 (418 views)
Permalink
Re: Native class methods [In reply to]

On 09 Oct 2007 17:45:12 +0200, Stefan Arentz <stefan.arentz [at] gmail> wrote:
>"Chris Mellon" <arkanes [at] gmail> writes:
>
>> On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <stefan.arentz [at] gmail> 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.
>> >
>> > 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)
>
>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 :-/
>

JNI is awful. I can't imagine why you'd want something like it. However,
since you do, why don't you just use the CPython/C API? It's the direct
equivalent of JNI (sorry, it's not quite as complex or horrible, though).

http://docs.python.org/api/api.html

Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list


arkanes at gmail

Oct 9, 2007, 9:41 AM

Post #6 of 6 (416 views)
Permalink
Re: Native class methods [In reply to]

On 09 Oct 2007 17:45:12 +0200, Stefan Arentz <stefan.arentz [at] gmail> wrote:
> "Chris Mellon" <arkanes [at] gmail> writes:
>
> > On 09 Oct 2007 17:20:09 +0200, Stefan Arentz <stefan.arentz [at] gmail> 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.
> > >
> > > 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)
>
> 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.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.