Replies: 11 comments 2 replies
-
Although I'm not the maintainer of this repository, I think I can answer your question to some extend, becuase I'm familiar with pybind11, which is similar to nanobind in the interface. It is hard to distinguish two python function by their signatures. There are several reasons:
So If you want to store python functions as callback functions (or for other usages), you'd better store them as And, of course, use 2 different function names to replace |
Beta Was this translation helpful? Give feedback.
-
@Yikai-Liao Thanks for your answer. I understand how difficult it could be. It is also important to report troubles in a priori legitimate use of the library. From an user point of view, I would have prefered a error stating that the Thanks also for your advices. The trouble is that I am wrapping an existing |
Beta Was this translation helpful? Give feedback.
-
If you want to specify a cpp function (May be those functions have been binded) on python side at runtime, the best way I think is to use some Enum to dispatch the right function in case you don't want to suffer a performance loss. Here I mentioned performance loss because even if you cast the binded cpp function to std::function, the function would be called through the python interpreter, which is slow. That's because the compiler just don't know wether the function you passed from python side is the cpp function above or a native python function.
Function PointerOne possible way is to write a lambda function in your binding codes (for non-intrusive binding to c++ libraries) like this: enum class MyFunc {};
// some codes to bind MyFunc
nanobind::class_<Test>(m, "Test")
.def("setFunction", [](Test& self, MyFunc tag) {
switch MyFunc {
case: // .... set the func
}}); If other prameters are used to create the function, I recommend you to use another function name in python. If you want to simplify the By the way, if the function is called frequenly, and it can be just treated as a function pointer, I recommend you to store the c++ function in the function pointer, which could significantly reduce the overhead. For example, there are 3 ways to store a lambda function (without capturing any arguments):
Then the overhead order is that If you need to specify the function at runtime, I think Class with Operator ()As I saided above, std::function is powerful, it could even store a class with customized operator(). So another possible way is to wrap the function you need into a class! struct MyFunc1 {
double operator() (double arg) { return arg + 1. ; }
}
// define other functions
nanobind::class_<MyFunc1>(m, "MyFunc1");
nanobind::class_<Test>(m, "Test")
.def("setFunction", [](Test& self, MyFunc1 func) { self.setFunction(func); })
.def("setFunction", [](Test& self, MyFunc2 func) { self.setFunction(func); }) In this way the function you binded can be dispatched by the typing system. |
Beta Was this translation helpful? Give feedback.
-
@Yikai-Liao Thanks ! That's interesting. For the moment, perfomance is not an issue: if the user wants to use a I am thinking of exposing to distinct (private) methods using the following design:
And in my |
Beta Was this translation helpful? Give feedback.
-
Thanks to the complexity of the typing system, you should still be able to come up with a lot of abstract solutions, as long as you don't worry about call overhead. You can explore which one best suits your project. |
Beta Was this translation helpful? Give feedback.
-
By the way, have you found any leaked types or leaked functions warnings in python 3.12 #397 ? |
Beta Was this translation helpful? Give feedback.
-
A lot ! I had to use |
Beta Was this translation helpful? Give feedback.
-
I don't know if I understand your problem correctly, but does the overload functionality https://nanobind.readthedocs.io/en/latest/classes.html#overloaded-methods work here? |
Beta Was this translation helpful? Give feedback.
-
@ThoreWietzke I will give a try, thanks for the tips. I did not notice this feature and basically used |
Beta Was this translation helpful? Give feedback.
-
@ThoreWietzke Unfortunately, it does not help. The implementation of |
Beta Was this translation helpful? Give feedback.
-
This does not look to me like an actual nanobind issue -- it's more of a gripe with the existing interface and C++ language itself. I will convert it to a discussion. |
Beta Was this translation helpful? Give feedback.
-
Problem description
Let us consider the following class:
It seems that exposing the two overloads of the
setFunction
method leads to a conflict, one hiding the other.Please find here a minimal test case: test.zip
Reproducible example code
The attached C++ file exposes the
Test
class and thepython
test fails with the following message:which indicates that the
python
lambda was passed to the wrong overload of thesetFunction
method.Beta Was this translation helpful? Give feedback.
All reactions