给定使用Boost.Python公开的C类,如何公开两个构造函数:
>一个采用numpy数组,和
>另一个需要python列表?
解决方法:
我不是100%的意思,但我假设你想让一个构造函数采用Python列表而另一个采用numpy数组.有几种方法可以解决这个问题.最简单的方法是使用make_constructor函数并重载它:
using boost;
using boost::python;
shared_ptr CreateWithList(list lst)
{
// construct with a list here
}
shared_ptr CreateWithPyArrayObject(PyArrayObject* obj)
{
// construct with numpy array here
}
BOOST_PYTHON_MODULE(mymodule)
{
class_ >
("MyClass", no_init)
.def("__init__", make_constructor(&CreateWithList))
.def("__init__", make_constructor(&CreateWithPyArrayObject))
}
标签:boost-python,python