Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-10-15 20:52:40

----- Original Message -----
From: <mats_s11_at_[hidden]>
> Hi,
> I am working in a large project to convert python code to C++,
> To save coding time we would like to keep all test programs in
> Python code and expose our new C++ classes to python through boost
> to be able to run all test cases.
> So far we have been very impressed by the functionality and design of
> boost but we have ran into two problems:
> 1. I have written a class in Cpp and exposed it to python,
> then I have another function in Cpp that should be called from python
> with a python list of Cpp objects of the class that I have exposed
> earlier.
> How do I do this?
> I can write a function taking a pythonlist as in arguments but I
> have not succeeded in extracting the object of my own class from
> this list...


Have your function take a boost::python::list argument. To extract your
object from an element of the list, you can follow this example:

MyObject& get(boost::python::list l, std::size_t n)
    return from_python(l[n].get(), boost::python::type<MyObject&>());

> 2. I have created a class with the "operator==" function in C++
> and exposed it to python and this works perfect except for one thing.
> when I want to use the python "None" object I get a type error.
> how can I deal with the "None" object in C++?

What do you want to do with "None"? (specific and clear questions always

If you want to use it as an argument to operator==(), you could try exposing
an overload for operator==() which takes a boost::python::ref parameter...
but then you would have to implement the runtime type checking yourself. How
exactly you would approach this depends on what method you've used to expose
operator==: __cmp__, __eq__, or boost::python::operators<...>.

> I would be extremely thnakful for any answers or help in any way.

I hope this helps, Mats.

BTW, I am currently offering professional support and development services
for Boost.Python, in case you are interested.

  David Abrahams, C++ library designer for hire

        C++ Booster (
          email: david.abrahams_at_[hidden]

Boost list run by bdawes at, gregod at, cpdaniel at, john at