Easier to wrap C or C++ libraries?

Chris Rebert clp2 at rebertia.com
Sat Feb 14 03:40:56 CET 2009


On Fri, Feb 13, 2009 at 5:35 PM,  <argo785 at gmail.com> wrote:
> When creating a Python binding to a C or C++ library, which is easier
> to wrap, the C lib or the C++ one? Given a choice, if you had to
> choose between using one of two libs, one written in C, the other in C+
> + -- both having approximately the same functionality -- which would
> you rather deal with from your Python code?
>
> It would seem to me that there's fewer design considerations when
> wrapping a library written in C; you just wrap the functions. However,
> since Python supports OOP nicely, it might also be that wrapping C++
> code *could* also be staightforward... Are there many pitfalls when
> having to map C++'s notion of OO to Python?

You're asking two separate questions here.

(1) For Python bindings, is it easier to wrap C, or C++ libraries?
As Daniel points out, C is easier to wrap because CPython itself is
written in C, and C++ cannot be linked directly to C.

(2) Is it easier to use C or C++ bindings from Python code?
Interface-wise, the C++ bindings would be easier to work with at the
Python level as they are object-oriented.

In practice, C libraries are usually wrapped because that's easier to
do. However, to compensate for their procedural interface, people
often write another module on top of the raw Python bindings to expose
a public interface that appears more object-oriented, and make the
lower-level module internal/private.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com



More information about the Python-list mailing list