[C++-sig] call Python function from C++ without copying the arguments
Sebastian Walter
walter at mathematik.hu-berlin.de
Sat Jan 17 12:04:57 CET 2009
Hello,
I want to call a Python function from C++ with an object of a C++ class
as argument.
The Python-function should change the argument, so changes are visible
on the C++ side.
Boost::Python automatically makes a copy of all the arguments that are
passed to a callable Python object.
Is there a way NOT to copy all arguments??
Below sample code:
The desired output of $python test.py would be
- ----------- desired output ---------------
a.x=0
a.x=12345
- ----------- test.cpp ---------------
#include <iostream>
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
class A{
public:
int x;
A(){x=0;}
};
void my_test_function(object fun_obj){
A a;
cout<<"a.x="<<a.x<<endl;
fun_obj(a);
cout<<"a.x="<<a.x<<endl;
}
BOOST_PYTHON_MODULE(_test)
{
def("my_test_function", my_test_function);
class_<A>("A", init<>())
.def_readwrite("x", &A::x);
}
- ----------- test.py ---------------
from _test import *
def f(a):
a.x = 12345
Thanks in advance,
Sebastian Walter
--
Sebastian Walter
Institut fuer Mathematik, Humboldt-Universitaet Tel: +49 (30) 2093-5869
Rudower Chaussee 25, Adlershof, Berlin Fax: +49 (30) 2093-5859
Post: Unter den Linden 6, D-10099 Berlin Email:
walter at mathematik.hu-berlin.de
WWW: http://www.mathematik.hu-berlin.de/~walter
More information about the Cplusplus-sig
mailing list