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
-
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
NodeFactory
and a derived classPythonNodeFactory
in Python. TheNodeFactory
class has a pure virtual functionload
that is overridden inPythonNodeFactory
.Here's a simplified version of my code:
C++ code:
Python code:
In this code,
create_node
calls the load method on aPythonNodeFactory
object, which returns aPythonNode
object. However, when I print the type of the returned object in Python, it printsNode
instead ofPythonNode
.I've confirmed that the correct
load
method 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
PythonNodeFactory
in Python?Beta Was this translation helpful? Give feedback.
All reactions