[C++-sig] how to pass a c++ object to python?

Roman Yakovenko roman.yakovenko at gmail.com
Sun Jan 15 07:29:19 CET 2006


On 1/13/06, Raul <ro4tub at gmail.com> wrote:

>
>  /// embed.cpp
>
> CEntity entity("oscar");
> /// call "FooWithArg(entity)" in the script file.
>
> i want to ask how to pass the object 'entity' to the foo.py!

Read this page: http://boost.org/libs/python/doc/v2/scope.html

bp::scope().attr("entity") = entity;

Take a look at pyplusplus code generator. For example this code has
been generated for
your code, using pyplusplus GUI:

namespace bp = boost::python;

BOOST_PYTHON_MODULE(pyplusplus){
    bp::class_< CEntity >( "CEntity", bp::init< std::string >((
bp::arg("name") ))[bp::default_call_policies()] )
        .def( "Name"
                , (::std::string ( ::CEntity::* )(  ) const)(&CEntity::Name)
                , bp::default_call_policies() );

    bp::scope().attr("entity") = entity;
}


Roman Yakovenko



More information about the Cplusplus-sig mailing list