覆盖__instanceCheck____ pybind11上课

发布于 2025-02-13 01:22:49 字数 901 浏览 0 评论 0原文

我需要覆盖使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文