| Basic Interface

Class `object` wraps `PyObject*`. All the intricacies of dealing with `PyObject`s such as managing reference counting are handled by the `object` class. C++ object interoperability is seamless. Boost.Python C++ `object`s can in fact be explicitly constructed from any C++ object.

To illustrate, this Python code snippet:

`

        def f(x, y):
             if (y == 'foo'):
                 x[3:7] = 'bar'
             else:
                 x.items += y(3, x)
             return x

        def getfunc():
           return f;

`

Can be rewritten in C++ using Boost.Python facilities this way:

`

        object f(object x, object y) {
             if (y == "foo")
                 x.slice(3,7) = "bar";
             else
                 x.attr("items") += y(3, x);
             return x;
        }
        object getfunc() {
            return object(f);
        }

`

Apart from cosmetic differences due to the fact that we are writing the code in C++, the look and feel should be immediately apparent to the Python coder.

Copyright © 2002-2003 David Abrahams
Copyright © 2002-2003 Joel de Guzman
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.