[SciPy-dev] Can I call Python-functions from """code"""?
eric at scipy.org
Thu Jan 24 11:57:56 CST 2002
> Is there a way to cll my self-defined Python-functions from the embeded C++
You can. Since code is all C++ code, and your wanting to call a Python function, you have to do two things:
1. Pass the function you want to call as an argument to inline.
2. Call the function using the Python API.
Take note that the call back into Python exacts a large performance toll, so you probably won't see much speed up.
> And if so, how???
Here is an example from weave/examples/functional.py that is similar to Python's builtin map(). There is a 2nd "prettier" version in functional.py, but it is half the speed of the standard map() function.
""" Uses Python API more than CXX to implement a simple map-like function.
It does not provide any error checking.
code = """
PyObject* item = NULL;
PyObject* this_result = NULL;
for(int i = 0; i < seq.length();i++)
item = PyList_GET_ITEM(seq.ptr(),i);
this_result = PyEval_CallObject(func,args.ptr());
return_val = Py::new_reference_to(result);
seq = ['aadasdf'] * 5
result = c_list_map2(len,seq)
If you can re-write the function you need to call as a C function, then you can call it directly in C to get the speed back. There are many cases where this isn't feasible, but some where it is. Use the support_code key variable for this. I think there are some examples of this in weave/examples/fibonacci.py
Hope that helps,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Scipy-dev