Overloading __instancecheck__
method of metaclass
#4039
Unanswered
nicola-gigante
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I need to overwrite the
__instancecheck__
method for a C++ class exported usingpybind11
. AFAIK this needs to be done on the class' metaclass, but I'm not familiar with the concept.Can I do that with
pybind11
?My initial attempt:
This does not work when I load the module:
If I had to write this in Python,
PippoMeta
would have to derive fromtype
. So maybe that's the missing bit, but how to define thepy::class_
to derive fromtype
?Edit:
So looking at the
pybind11
implementation it seems that registered classes must havepybind11
special default metaclass. So I tried to make my metaclass inherit from that, but this triggers anassert
inpybind11
:The output:
Looking at the failed assertion, it seems that
pybind11
expects any registered class to have at least a registered base class. That's whyget_type_info
onPippoMeta
's base class returnsnullptr
and the assert triggers. Is my conjecture right?Note: cross-posted with StackOverflow
Beta Was this translation helpful? Give feedback.
All reactions