我有一个用C ++编写的内部库,目前正在研究将其扩展到Python中。我在开始时就考虑了Boost.Python,但是我愿意接受其他选择。
当前,我有一个C ++函数,需要接受一个Python类实例,然后使用该对象的方法执行某些任务。这个想法是让Python用户永远不需要处理C ++。期望他们从我将提供的Python模板/示例类中创建此Python对象,并使用我可以假设在C ++库中存在的预设方法名称。
暴露给Python用户的界面如下所示:
class Foo(object):
def __init__(self, args):
"""create an instance of this class with instance-specific attributes"""
def Bar1(self, a, b, c):
"""do something with the given integers a, b and c"""
pass
def Bar2(self, a, b, c):
"""do something else with the given integers a, b and c"""
pass
import mylib
cheese = mylib.Wine()
cheese.do_something(Foo)
Run Code Online (Sandbox Code Playgroud)
在C ++中,相应的代码如下所示:
#include <boost/python.h>
#include <Python.h>
class Wine {
public:
Wine() {};
~Wine() {};
static void do_something(boost::python::object *Foo) {
int …Run Code Online (Sandbox Code Playgroud) boost-python ×1