Issue with dynamic type information when returning derived class object from C++ to Python #4969
Unanswered
jeongseok-meta
asked this question in
Q&A
Replies: 1 comment
-
|
Hello I think I have a similar problem and I'm seeking guidance! In my case my setup is similar to yours but I have the problem that when I use .def("__getitem__", [](const DataFrame &df, const KeyType &key) {
return df(key);
})The instance returned is always from the original c++ class instead of the For instance I use the python class directly like class DataFrame(CppDataFrame):
...
# I use it as
df = DataFrame(...)
# (...)
print(type(df), type(df['x']))
>>> <class 'statistics_extension.DataFrame'> <class 'statistics_bindings.CppDataFrame'>as you can see I cannot preserve the |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
I'm having an issue with pybind11 where the dynamic type information of an object seems to be lost when the object is returned from C++ to Python. I have a base class
NodeFactoryand a derived classPythonNodeFactoryin Python. TheNodeFactoryclass has a pure virtual functionloadthat is overridden inPythonNodeFactory.Here's a simplified version of my code:
C++ code:
Python code:
In this code,
create_nodecalls the load method on aPythonNodeFactoryobject, which returns aPythonNodeobject. However, when I print the type of the returned object in Python, it printsNodeinstead ofPythonNode.I've confirmed that the correct
loadmethod is being called, so it seems like the issue is with how the return type is being handled.Does anyone know how I can preserve the dynamic type information of the returned object so that it is correctly recognized as a
PythonNodeFactoryin Python?Beta Was this translation helpful? Give feedback.
All reactions