What's your first choice if you have to write a C module for python?

B

bearophileHUGS

(e-mail address removed):
I read this interesting post comparing Boost.Python with Pyd:
http://pyd.dsource.org/vsboost.html
What's your opinion about it?
What's your first choice when you have write a C/C++ module for Python?

That's not exactly a post.
Pyd works well enough, it's easy to use and requires very little extra
code for the interface, I've used it several times. But it's designed
for D, not for C/C++, so in some situations it can be useless. For C/C+
+ you can find any kind of lib to wrap with Swig/ SIP/ Boost Python/
Cython/ etc, but you can't find much for D yet. And the D community is
very small, so there's just 1 person that updates Pyd, so updates are
slow, support limited (but it exists, on the #D IRC channel) and so
on. D surely isn't as complex as C++, and it's way nicer, but it's not
simple either, so learning it may require some time. Another thing to
think about is that C code sometimes may be even two times faster than
D code, so if max speed is essential, then D may not be the best
choice.

Bye,
bearophile
 
U

Uwe Schmitt

Hi all,

I read this interesting post comparing Boost.Python with Pyd:

http://pyd.dsource.org/vsboost.html

What's your opinion about it?

What's your first choice when you have write a C/C++ module for Python?

I prefer ctypes or f2py. The latter works with C-code too:
Look at http://www.procoders.net/?p=189
f2py is nice if you have to process numerical data, ctypes is
more general for interfacing shared libs and is contained
in Python2.5s standard lib.

Both solutions are more lightweight than Boost which I used some
years ago, but found to complex when building extensions
on different platforms.

Normally I implement basic functionality in a DLL/shared lib
which is wrapped by a Python class to provide pythonic look
and feel.

Greetings, Uwe
 
U

Ulrich Eckhardt

一首诗 said:
I read this interesting post comparing Boost.Python with Pyd:

http://pyd.dsource.org/vsboost.html

What's your opinion about it?

What's your first choice when you have write a C/C++ module for Python?

There is no such thing as a C/C++ language. Seriously, both are really
different and require different approaches. That said, when writing a C++
module I use Boost.Python, for one because Boost is already part of many
C++ projects but also because it is simply very easy and convenient to use.

There is another alternative, ctypes, which also works but it IMHO brings
many of the disadvantages of C to Python. In particular those are manual
resource management and lack of exceptions. Looking at Boost.Python though,
you can simply throw a C++ exception and it gets mapped to a Python
exception, and resource management is also a non issue since you can make
ownership transfers explicit.

Now, concerning pyd, I think you are comparing apples to oranges. If you had
asked which language I would chose to write a Python plugin, that would
have been a totally different thing. To that, the answer is in order of
preference:
1. Python ;)
2. C++ with Boost.Python
3. C with ctypes

cheers

Uli
 
C

Carl Banks

What's your first choice when you have write a C/C++ module for Python?


1. Plain C code
2. Implement my own C extension code generator in Python
3. ctypes
...
...
Infinity. SWIG


Carl Banks
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top