I'm trying to bind C++ class static non-arguments method to python class static constant field use pybind11.
Here's my sample code 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);
}
The config module compiles successfully, but when I use it in python3 console, here's the exception it raise:
>>> 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'>
How should I fix this ?
Or is there a way to bind C++ function to python module constant attributes/variables ?