Hi,

On Wed, Jun 24, 2009 at 7:52 AM, Christopher Schramm
<cschr...@shakaweb.org> wrote:
>
> Stefan Seefeld wrote:
> > An alternative is not to use BOOST_PYTHON_MODULE at all, but set up
> > converters in ordinary C++ code. In the following I set up a Python
> > interpreter in my main application, inject a (C++) base class, run a
> > Python script that adds a derived class, then instantiate and run that
> > derived class from C++ code. I this may just be what you want:
>
> Sure. That's what I'm doing (just within a submodule below main). But in
> your example - how do get simple c++ functions into the python scope
> which aren't members of any class? That's my problem.

This seems to work:

#include <boost/python.hpp>

#include <iostream>

namespace bp = boost::python;

void myfunction() {
    std::cout << "Hello world!\n";
}

int main(int argc, char* argv[]) {
    Py_Initialize();

    bp::object main = bp::import("__main__");
    bp::object global = main.attr("__dict__");

    bp::object function = bp::object(myfunction);
    global["function"] = function;
    bp::exec("function()", global, global);
    return 0;
}


Is this what you wanted?

Boost python's def returns void, which is natural I think, since you
can't assign to the "result" of def in python either.

Hope this helps,
Thomas
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to