6

我有一组用 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);
}

当然,这是行不通的。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);
}

理想情况下,我想将所有这些自定义实用程序类型和相关函数保存在一个单独的模块中,与所有使用它们的模块分开。但是我需要在使用它的每个模块中定义类型转换,或者以其他方式引用它。我该怎么做?我不想要pb11_example_module.Color等等utils.Color。我不知道它们的兼容性,这似乎是错误的方式。

4

1 回答 1

4

这开始是一个编辑,但结果是我的答案。

所以这很有趣。使用Colorpybind 模块中未导出的第一个示例...

$ 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'

在导入示例模块之前导入实用程序库也可以。将类名更改为"Color"其他名称也不会破坏使用,因此它必须使用类型签名从其他模块获取类型转换。

只要在使用前定义了该 C++ 类型的类型转换,自动类型转换就可以工作。pybind 用于 Python 的类型转换实用程序是全局的,并在运行时查找。您可以在此处阅读所有相关信息。上述在使用前的任何时候为自定义类型加载类型转换的解决方案是受支持的惯用解决方案。

于 2018-08-15T03:04:22.900 回答