How to bind C++ non-arguments function to Python constant attributes/variables ? #3575
-
Hi, I'm trying to bind C++ class static non-arguments method to python class static constant field. Here's my sample code 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:
How should I fix this ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Or is there a way to bind C++ function to python module constant attributes/variables ? |
Beta Was this translation helpful? Give feedback.
-
I found answer here: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#static-properties. use But I still have no idea about how to bind C++ non-arguments function to Python constant attributes/variables. |
Beta Was this translation helpful? Give feedback.
I found answer here: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#static-properties. use
def_property_readonly_static
with lambda could bind C++ class static non-arguments method to Python class static constant attributes/variables.But I still have no idea about how to bind C++ non-arguments function to Python constant attributes/variables.