[boost.python] How to get derived class from base class in Python?

This may potentially be unrelated to Boost.Python, but how to do an
equivalent of C++ dynamic_cast in Python, having all the classes wrapped
using boost.python? See below:
// All the components inherit from Component
class Component { };
class ComponentA: public Component { };
class ComponentB: public Component { };
// Find a component by name
Component *FindComponent(const char *name);
// In C++ result of FindComponent may be dynamic_casted to appropriate type:
void f()
{
Component *c = FindComponent("a");
ComponentA *a = dynamic_cast

I withdraw the question. def f(): c = FindComponent("a") c will actually have methods of ComponentA, for some reason this did not seem to work for me for the first time, but now it does. sorry, Marcin
This may potentially be unrelated to Boost.Python, but how to do an equivalent of C++ dynamic_cast in Python, having all the classes wrapped using boost.python? See below:
// All the components inherit from Component class Component { }; class ComponentA: public Component { }; class ComponentB: public Component { };
// Find a component by name Component *FindComponent(const char *name);
// In C++ result of FindComponent may be dynamic_casted to appropriate type: void f() { Component *c = FindComponent("a"); ComponentA *a = dynamic_cast
(c); } How to do this in Python (all Component classes are wrapped)?
def f(): c = FindComponent("a") # Unforunately Python sees c as an instance of Component, not as ComponentA which it really is a = ???
Thanks, Marcin
participants (1)
-
Marcin Kalicinski