【发布时间】:2022-01-26 22:17:25
【问题描述】:
我正在尝试将 C++ 类静态非参数方法绑定到 python 类静态常量字段使用 pybind11。
这是我的示例代码config.cpp:
namespace py = pybind11;
struct Env {
static std::string env() {
return std::getenv("MY_ENV");
}
};
PYBIND11_MODULE(config, m) {
m.doc() = "my config module written in C++";
py::class_<Env>(m, "Env")
.def_property_readonly_static("ENV", &Env::env);
}
config 模块编译成功,但是当我在 python3 控制台中使用它时,它引发了以下异常:
>>> from config import Env
>>> Env.ENV
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: (): incompatible function arguments. The following argument types are supported:
1. () -> str
Invoked with: <class 'config.Env'>
我应该如何解决这个问题?
或者有没有办法将 C++ 函数绑定到 python 模块常量属性/变量?
【问题讨论】:
标签: python c++ static-methods pybind11