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

Craig Finch oanjao at yahoo.com
Wed Jun 27 20:06:08 CEST 2007


Thank you for the suggestion--unfortunately it does not seem to work
for me.  In my Python code, I verified that the list is composed of
different types of objects.  However, in C++ I tried

std::cout << typeid(extract<baseclass&>(myList[j])).name();

and it shows that all of the extracted objects are of the same class
(baseclass).  Any other ideas?  I want to be able to do the following
in C++:

for (i=0 to length of list)
   list[i].evaluate();

where the "evaluate" method is different depending on what kind of
object is pulled from the list.

   Craig Finch

--- Roman Yakovenko <roman.yakovenko at gmail.com> wrote:

> On 6/27/07, Craig Finch <oanjao at yahoo.com> wrote:
> > I have run into a problem that I don't know how to solve with
> > Boost.python, and I can't find the answer online.  I have a list of
> > objects in Python, and the objects are of different classes.  All
> the
> > classes are derived from a common base class, and the classes are
> > defined in C++ and exposed to Python.  The list of objects is
> created
> > in Python, and I would like to get this list into C++, where the
> the
> > objects of different types will be handled differently.  I can
> bring
> > the list into C++ as a boost::python::list, but then I have to use
> > extract <type> to get the individual objects from the list.  The
> > problem is that extract<baseclass> works, but then all of the
> extracted
> > objects are the same type.  The information contained in the
> derived
> > types is lost.
> 
> Try to extract "baseclass&".
> 
> -- 
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> 


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


      ___________________________________________________________________________________
You snooze, you lose. Get messages ASAP with AutoCheck
in the all-new Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_html.html



More information about the Cplusplus-sig mailing list