views:

1257

answers:

3

I feel as though this question is a duplicate, but I haven't found one on the subject (specifically) yet so I'll ask anyway:

What Python/C++ binding libraries would you recommend and why? In addition, what has been your experience using it or any others you have tried? Also, what binding features do they contain and has your use of them been personal or professional?

To kick things off, here are some that I've tried recently (for personal use):

SWIG

  • Supports Python 2 and 3
  • Can be uber-automatic under the right circumstances
  • When it's not uber-automatic it mostly consists of repeating your .h files and providing hints
  • Supports many other languages besides Python (Java, Ruby, Lua, etc.)
  • Output consists of both a native file (to be compiled into a .pyd) and a python "wrapper"
  • Bindings appear to be reasonably lean.
  • Does not appear to support properties (values accessed by getter/setters)
  • Very well documented, easy to follow setup instructions
  • Used by Google (hey, that's got to count for SOMETHING, right?)

Boost::Python

  • Only supports Python 2. Python 3 support is in progress, but no release date yet. (as of Sept 09)
  • Syntax can be awkward to newcomers, but is straightforward and mostly clutter free.
  • Heavy use of C++ templates (can be a good or bad thing)
  • Distributed as part of the Boost library, which is huge. (Useful, but huge)
  • Compiling the library initially can be an exercise in frustration
  • Can significantly increase compile times
  • Some quirky gotchas, like specifying return value policies for functions that return native types
  • Very solid, stable, well-tested library
  • Does support properties
  • Documentation is so-so. Decent introductory tutorials but more advanced usage is somewhat neglected. Documentation is also fragmented: There are at least three different tutorials on how to build the library, all of which differ greatly.
  • Bindings have a reputation for being somewhat bloated

I'm currently using Boost::Python, mostly because I really need property support, but I'm very curious to see what people's opinions are of some of the other libraries out there!

+1  A: 

You can have a look at pyCxx which is in my opinion a very nice library. I can't make a real comparison with Boost::Python or SWIG but I think that it less heavy that Boost and easier to use than SWIG.

I've used it in the past and was very happy with that choice. It was easy to make a python extension. In particular, I've enjoyed the provided examples. It was easy to get started compared to Boost and SWIG that I've tried before.

The project page says that Python 2 and 3 are supported. I've only used it with Python 2.

I would be interested to read your evaluation of pycxx. The ones you've made for Boost and SWIG are very interesting.

luc
+1  A: 

I like SWIG a lot for Python. Best slides ever:

http://www.dabeaz.com/SwigMaster/index.html

If you can make it through those, you'll have a pretty good sense of SWIG. There's a small typo on slide I-12 (I think). But yeah, Beazley is a genius (and very clear at explaining this and other parts of Python).

Recently I modified pyglfw (which uses SWIG) to create a new method glfwPlatformGetWindowPos. It was pretty straight-forward after reading those slides and getting the hang of SWIG.

But the nice thing about SWIG is that it really embraces the idea of a parse tree. This allows all sorts of interesting features (similar to lisp macros) that really improve the whole process (e.g., generating 'decorators' throughout the tree, which is arguably a big part of interfacing between the two languages). SWIG is quite elegant if you ask me.

I haven't used Boost.Python yet, but I have used Boost a lot. And they have a lot of good C++ libraries. But there is a little bit of a tendency to bloat via template-metaprogramming (as the OP mentions). It's like ACE targeting multiple platforms. Or Javascript targeting multiple browsers. The thing just gets unfocused after a while (in their case, targeting multiple compilers). Maybe Boost.Python is an exception though. But if you think about it, SWIG has been 'breadth-tested' with multiple languages. If your C++ code already exists and is heavy with templates though...maybe Boost is worth considering (that's how I see it anyways).

Do you know of any way to add Python properties with SWIG? I really like how it works, but that feature is something of a must have for me and I can't find any (sane) way of doing it.
Toji
That's an excellent slideshow, BTW! Thanks!
Toji
that's a good question. i'm no SWIG expert by any means, but it appears to set properties by default for all public data variables in a C++ class/struct. If you're curious, search for 'property' in Source/Modules/python.cxx of the SWIG source. For mapping actual get/set functions from C++ into Python, you might have to modify that python.cxx (if passing in the -shadow isn't doing it). Maybe Boost.Python is better for that. Ideally, there's probably some macro-like thing you can use in SWIG to treat certain C++ methods (e.g., those that begin with set or get) as Python properties.
+4  A: 

I also would look into the more automated (usually gccxml-based) solutions. Py++ is a good place to start, it creates boost::python bindings from the library source and an interface description.

JPVDB