If in C++ a class had an overloaded constructor:
class MyClass {
std::string name;
public:
MyClass(){};
MyClass(std::string name_){ name = name_;}
};
how do I expose this to python? Can I use py::overload_cast and py::init together?
Currently I expose the first one:
PYBIND11_MODULE(ex_module, module_handle){
module_handle.doc() = "ex class";
py::class_<MyClass>(module_handle, "PyEx")
.def(py::init<>())
;
}
(Note that I'm using C++20 and python >=3.10)
I want to be able to do:
new_obj = PyEx()
new_obj = PyEx("blah")
Currently, PyEx("blah") isn't throwing any errors, but it's also not really passing blah into the C++ constructor. I check this by adding a print statement in the second constructor.
Edit: simplified the question.
Exto take a single argumentkeyword_arg... then in the callEx("blah", keyword_arg="foo")assign it two different values -- first the positional"blah", second the named"foo".