-
Notifications
You must be signed in to change notification settings - Fork 216
Closed
Description
Hi,
Please can you help explain how a user defined C++ class instance can be passed as an argument to a Python function. I am able to pass an integer argument using the PyInt_FromLong function as below.
int MyClass::getSquare( int x )
{
return pow( x, 2 );
}
BOOST_PYTHON_MODULE( Test )
{
class_<MyClass>( "MyClass" )
.def( "getSquare", &MyClass::getSquare )
;
}
PyObject * py_args_tuple = PyTuple_New(1);
PyObject * py_int;
py_int = PyInt_FromLong( 4 );
PyTuple_SetItem( py_args_tuple, 0, py_int );
PyObject_CallObject( pFun, py_args_tuple );
def getSquare( x ):
// Create MyClass object myClass
print myClass.getSquare(4)
Thanks,
Nilufar
Metadata
Metadata
Assignees
Labels
No labels