Say I have a simple C++ class
class MyClass{
//constructor
MyClass(int value):_value(value){};
void operator()(AnotherClass& const b){
// Do something with b object
}
private:
int _value;
}
I am trying to create Python binding of this class using pybind11. How can I bind the operator()
method?
This binding is going to be used to pass objects of this class to functions requiring a callback function as an argument.
I figured this out after trying various things. We need to define the __call__
method in our binding. As an example:
.def("__call__", [](MyClass& this, AnotherClass& const b){
return this(b);
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With