[C++-sig] C++ call python class

David Abrahams dave at boost-consulting.com
Fri Jul 22 16:08:17 CEST 2005


Yongsheng Yang <yys2000 at yahoo.com> writes:

> hi all:
>
> Right now, I want to use C++ to call a python class.
> There is a sample or tutorial here?

    // Retrieve the Python module called "some_module"
    python::object some_module((
        python::handle<>(python::borrowed(PyImport_AddModule("some_module")))));

    python::object some_class = some_module.attr("some_class");

    // assuming __init__ takes only a "self" argument
    python::object instance_of_some_class = some_class();

    // call instance_of_some_class.foo(3.14)
    instance_of_some_class.attr("foo")(3.14);

HTH
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list