I have a C++ code which depends on a large library (OpenBabel to be concrete) and uses some of its classes as bases for my classes. This library has its own Python bindings created with SWIG. I've built Python bindings with SWIG for my code with '%import' of this library's interface. When it comes to passing my classes to the library's routines in Python code this works fine. However I experience problems with retrieval of my classes form library's routines.
To clarify the idea I'll show some code. For the C++ version
class Derived: public Library::Base
{
// blah-blah
std::string toString();
};
// the library has function, returning Base*
Library::Base* getData();
// In C++ code I siply use
Derived *d = static_cast<Derived*> (getData());
std::cout << d.toString();
Now I want to do the same thing in Python:
d = getData();
print( d.toString() )
This results in an error: AttributeError: 'Base' object has no attribute 'toString'
print(d)
results in <Library.Base; proxy of <Swig Object of type 'std::vector< Library::Base * >::value_type' at 0x7f0ff8279a20> >
so this is a wrapper of C++'s Base*
I've found this advice for upcasting: d.__class__ = Derived
, however this looks strange for me. Moreover, it does not work.
So how should I upcast in Python, or what am I doing wrong?