-
In C++, we can convert a Python function to std::function. class CClass
{
std::function<int(int)> f;
void set_f(const std::function<int(int)>& f_)
{
f = f_;
}
};
NB_MODULE(gurobi_model_ext, m)
{
nb::class_<CClass>(m, "CClass").def(nb::init<>()).def(set_f, &CClass::set_f);
} class PClass:
def __init__(self):
self.x = 1
def g(y):
return x + y
P = PClass()
C = CClass()
C.set_f(P.g) I am not sure whether nanobind will ensure the liveness of |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
Apr 16, 2024
Replies: 1 comment 1 reply
-
Nanobind will keep the object alive to satisfy the dependency, if that's what you are asking. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
metab0t
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nanobind will keep the object alive to satisfy the dependency, if that's what you are asking.