Is there a way to pass a python function ptr to a c++ method from a python script?

Paul McGuire ptmcg at
Tue Jun 6 04:32:32 CEST 2006

"liam_herron" <liam_herron at> wrote in message
news:1149555886.251441.188180 at
> I have a core c++ library that is exposed to python through the
> boost_python framework.
> I would like to write the core of a Newton's method solver in C++ and
> be able to write the
> functions that are evaluated to be in python.   Does anyone have any
> ideas on this?

My suggestion would be to prototype in all-Python first.  This will let you
work out the kinks of your call/callback interfaces.  Then see if the
Newton's method part is a bottleneck worth converting to C++.  My suspicion
is that the performance (for all but trivial functions) will be in the
function you are solving for, not in the Newton's metod.  So be sure to test
with a function that you feel is representative of those you want to solve
for, not just a simple parabola.

Then, stop thinking in C++.  "Function pointer"?   A function in Python is
an object, a callable.  You should be able to just treat it like one.

-- Paul

More information about the Python-list mailing list