ktb*_*ktb 5 c++ python pybind11
我有一组用 C++ 编写并使用 pybind11 导出到 Python 的模块。所有这些模块都应该能够独立使用,但它们使用在实用程序库中定义的一组通用自定义类型。
在每个模块中都有类似于下面的代码。的Color.hpp报头定义了在实用程序库所使用的类型。
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
#include "Color.hpp"
std::vector<Color> buncha_colors(int n, std::string &color) {
std::vector<Color> out;
for (;n-- > 0;) {
out.push_back(Color(color));
}
return out;
}
PYBIND11_MODULE(pb11_example_module, m) {
m.def("buncha_colors", &buncha_colors);
}
Run Code Online (Sandbox Code Playgroud)
当然,这行不通。Pybind 不知道如何对Color对象进行类型转换。答案(或希望不是)是将Color类定义为模块的一部分。之后,pybind 能够进行自动类型转换。
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
#include "Colors.hpp"
std::vector<Color> buncha_colors(int n, std::string &color) {
std::vector<Color> out;
for (;n-- > 0;) {
out.push_back(Color(color));
}
return out;
}
PYBIND11_MODULE(pb11_example_module, m) {
pybind11::class_<Color>(m, "Color")
.def(pybind11::init<std::string&>())
.def("name", &Color::name);
m.def("buncha_colors", &buncha_colors);
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望将所有这些自定义实用程序类型和相关函数与所有使用它们的模块放在一个单独的模块中。但是我需要在使用它的每个模块中定义类型转换或以其他方式引用它。我该怎么做?我不想pb11_example_module.Color和utils.Color等。我不知道它们之间的兼容性,这似乎是错误的方式。
这最初是一个编辑,但后来变成了我的答案。
所以这很有趣。使用第一个示例,其中Color未在 pybind 模块中导出...
$ python
>>> import pb11_example_module
>>> pb11_example_module.buncha_colors(10, "red")[0].name()
TypeError: Unable to convert function return value to a Python type! The signature was
(arg0: int, arg1: str) -> List[Color]
>>> import utils # defines Color
>>> pb11_example_module.buncha_colors(10, "red")[0].name()
'red'
Run Code Online (Sandbox Code Playgroud)
在导入示例模块之前导入实用程序库也可以。将类名更改"Color"为其他名称也不会破坏用法,因此它必须使用类型签名从其他模块获取类型转换。
只要在使用之前定义了该 C++ 类型的类型转换,自动类型转换就会起作用。pybind 用于 Python 的类型转换实用程序是全局的并在运行时查找。你可以在这里读到所有和它有关的。上述在使用前随时加载自定义类型的类型转换的解决方案是受支持的惯用解决方案。