覆盖__instanceCheck____ pybind11上课
我需要覆盖使用pybind11
导出的C ++类的instanceCheck __
方法。 Afaik这需要在班级的元素段上完成,但我对这个概念不熟悉。
我可以用pybind11
做到这一点吗?
编辑:
我的初始尝试:
py::class_<Pluto> plclass(m, "Pluto");
struct PippoMeta { };
py::class_<PippoMeta> pimeta(m, "PippoMeta");
py::class_<Pippo> piclass(m, "Pippo", py::metaclass(pimeta));
plclass.def(py::init());
piclass.def(py::init());
pimeta.def("__instancecheck__", [](PippoMeta &, Pluto) {
py::print("Here");
return true;
});
加载模块时,这无效:
Traceback (most recent call last):
File "test.py", line 3, in <module>
from my_module import *
ImportError: Pippo: PyType_Ready failed (AttributeError: mro)!
如果我必须在python中编写它,则pippometa
应源自type
。因此,也许这是丢失的位,但是如何定义py :: class _
从类型
派生?
I need to overwrite the __instancecheck__
method for a C++ class exported using pybind11
. AFAIK this needs to be done on the class' metaclass, but I'm not familiar with the concept.
Can I do that with pybind11
?
Edit:
My initial attempt:
py::class_<Pluto> plclass(m, "Pluto");
struct PippoMeta { };
py::class_<PippoMeta> pimeta(m, "PippoMeta");
py::class_<Pippo> piclass(m, "Pippo", py::metaclass(pimeta));
plclass.def(py::init());
piclass.def(py::init());
pimeta.def("__instancecheck__", [](PippoMeta &, Pluto) {
py::print("Here");
return true;
});
This does not work when I load the module:
Traceback (most recent call last):
File "test.py", line 3, in <module>
from my_module import *
ImportError: Pippo: PyType_Ready failed (AttributeError: mro)!
If I had to write this in Python, PippoMeta
should derive from type
. So maybe that's the missing bit, but how to define the py::class_
to derive from type
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论