In defence of boost::python
, given Alexander's answer on ctypes:
Boost python provides a very "c++" interface between c++ and python code - even doing things like allowed python subclasses of c++ classes to override virtual methods is relatively straightforward. Here's a potted list of good features:
- Allow virtual methods of C++ classes to be overridden by python subclasses.
- Bridge between
std::vector<>
, std::map<>
instances and python lists and dictionaries (using vector_indexing_suite
and map_indexing_suite
)
- Automatic sharing of reference counts in smart pointers (
boost::shared_ptr
, etc) with python reference counts (and you can extend this to any smart pointer).
- Fine grained control of ownership when passing arguments and returning values from functions.
Basically, if you have a design where you want to expose a c++ interface in a way faithful to the language, then boost::python is probably the best way to do it.
The only downsides are increased compile time (boost::python makes extensive use of templates), and sometimes opaque error messages if you don't get things quite right.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…