[C++-sig] Extracting C++ objects of different types from a Python list

Craig Finch oanjao at yahoo.com
Wed Jun 27 23:28:27 CEST 2007


> If you could post a minimal, complete example, it would be easier to 
> diagnose the problem.

Here is a simple example of what I am trying to do.  There is no
problem if I know in advance the class of the objects to be imported. 
However, I don't know the class until runtime, so I don't know how set
the appropriate class for extract <???>.

---------------- C++ CODE---------------
class BaseClass {
    public:
    virtual void evaluate(void);
};

class A : public BaseClass {
    public:
    int index;
    virtual void evaluate(void);
};

class B : public BaseClass {
    public:
    float value;
    virtual void evaluate(void);
};

class Importer {
    public:
    void setList (boost::python::list l);
};

void BaseClass::evaluate (void) {
    std::cout << "Useless base class" << std::endl;
}
void A::evaluate (void) {
    std::cout << "Index is " << index << std::endl;
}
void B::evaluate (void) {
    std::cout << "Value is " << value << std::endl;
}
using namespace boost::python;

void BaseClass::evaluate (void) {
    std::cout << "Useless base class" << std::endl;
}
void A::evaluate (void) {
    std::cout << "Index is " << index << std::endl;
}
void B::evaluate (void) {
    std::cout << "Value is " << value << std::endl;
}
void Importer::setList (list l) {
    BaseClass o;
    int len = extract<int>(l.attr("__len__")());
    
    for (int i=0; i<len; i++) {
        std::cout << typeid(extract <BaseClass&> (l[i])).name() <<
std::endl;

        o = extract <BaseClass&> (l[i]);
        o.evaluate();
    }
}

BOOST_PYTHON_MODULE(C_test) {
    
    class_<BaseClass> ("BaseClass");

    class_<A, bases<BaseClass> > ("A")
        .def("evaluate", &A::evaluate)
        .def_readwrite("index", &A::index)
    ;
    class_<B, bases<BaseClass> > ("B")
        .def("evaluate", &B::evaluate)
        .def_readwrite("value", &B::value)        
    ;
    class_<Importer> ("Importer")
        .def("setList", &Importer::setList)
    ;
}

-------------- PYTHON CODE ------------------
import C_test

# Derived Classes
class A (C_test.A):
    def __init__ (self, int):
        C_test.A.__init__(self)
        self.index = int

class B (C_test.B):
    def __init__ (self, float):
        C_test.B.__init__(self)
        self.value = float

mixedList = []
mixedList.append(A(3))
mixedList.append(B(5.234))

imp = C_test.Importer()
imp.setList(mixedList)

--------------- OUTPUT ----------------
N5boost6python7extractIR9BaseClassEE
Useless base class
N5boost6python7extractIR9BaseClassEE
Useless base class

---------------------------------------

--------------
Please reply to cfinch at ieee.org


 
____________________________________________________________________________________
Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
http://tools.search.yahoo.com/toolbar/features/mail/



More information about the Cplusplus-sig mailing list