-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support classmethod #1693
Comments
@YannickJadoul were you going to look into this? This is a possible workaround without calling CPython API: class Example {
int x;
Example(int x) : x(x) {}
public:
static Example make_x(int y) { return Example(y); }
int get_x() const { return x; }
};
PYBIND11_MODULE(tmp, m) {
py::class_<Example>(m, "Example")
.def("get_x", &Example::get_x)
.def_property_readonly_static("make_x", [](py::object){
return py::cpp_function([](int x){return Example::make_x(3);
});});
} |
@henryiii, I was. I thought it was easy as the |
@YannickJadoul Pretty sure it should be a py::type. |
If follow the implementation of property_readonly_static though it does eventually call the C++ API. We should just officially support it IMO as this is a lot of indirection and all our static methods call attr anyway. |
Turns out this might actually be a bit easier than I thought. new is not ACTUALLY a classmethod as classmethod even though it is one of the most common usecases for them. As such, the overloading of new with classmethod is generating errors since we don't allow overriding an instance method with a static method or vice versa. That means implementing it shouldn't be too bad. The harder part is writing the appropriate tests / usecases. Especially any use cases where a static method would not work well currently. I would like to see an actual usecase where though where the class parameter of the classmethod is used though. As for the callsign, the class argument to the classmethod can just be a py::object. |
I have a workaround to create classmethods in PyBind.
used like this
Be useful to have such mechanism in pybind proper
The text was updated successfully, but these errors were encountered: