C API: how to replace python number object in place?

Carl Banks pavlovevidence at gmail.com
Fri May 15 00:14:45 EDT 2009

On May 14, 8:24 pm, vava... at cpu111.math.uwaterloo.ca (Stephen Vavasis)
> In my previous posting, I inquired how to change a python numeric object
> in place.  Several people responded that this is not possible.  Perhaps I
> should explain the larger problem that I am trying to solve, and then the
> solution will become apparent.  I have a C routine R that invokes a Python
> routine S repeatedly.  This Python routine S takes three arguments, two
> floats and an integer.  I have read the documentation explaining how R can
> use the C API to construct an argument list for invoking S.  The issue
> that baffles me is that, as far as I can tell, each time R invokes S
> again, it needs to deallocate and reallocate the three arguments.

That's correct.

(Well, not exactly: sometimes PyInt_New can use a pre-existing object
rather than to allocate a new one.  Also ints and floats I think both
use arena allocation, meaning that Python gets big chunks and creates
objects out of that.  It's not like malloc gets called for every
PyInt_New invokation.  But those are implementation details.  Just
trust that Python's been pretty well optimized by now to create new
int and float objects.)

> It
> would seem be much more efficient if R could create the argument list once
> and then modify the values inside of it for each subsequent invocation of
> S.

It really won't make that much of a difference.  Python is creating
and destroying objects all the time.  If you were to execute a line
like this in Python:

a = b + 2.0 / (0.5 * m * v * v)

It'd probably create and delete 5 intermediate number objects.
Whatever small gains you could make by eliminating a few object
allocations in your calling code would hardly be noticeable.

Carl Banks

More information about the Python-list mailing list