Search code examples
pythonc++static-methodspybind11

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 ?


Solution

  • 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.