Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pybind11 py::class_.def_property_readonly_static incompatible function arguments for () -> str

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 ?

like image 584
linrongbin Avatar asked Aug 26 '26 12:08

linrongbin


1 Answers

Here's the answer about how to bind C++ class static non-arguments method to python class static constant attribute/variable with def_property_readonly_static API: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#static-properties

The key is to use C++11 lambda, so update PYBIND11_MODULE part in config.cpp like this:


PYBIND11_MODULE(config, m) {
  m.doc() = "my config module written in C++";
  py::class_<Env>(m, "Env")
    .def_property_readonly_static("ENV", [](py::object /* self */){ return Env::env(); });
}

For the second question, how to bind C++ non-arguments function to python constant attributes/variables, I still have no idea.

like image 169
linrongbin Avatar answered Aug 29 '26 00:08

linrongbin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!