Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   C API: how to replace python number object in place? (http://www.velocityreviews.com/forums/t684048-c-api-how-to-replace-python-number-object-in-place.html)

Stephen Vavasis 05-14-2009 05:25 PM

C API: how to replace python number object in place?
 
If x is a C variable of type PyObject*, and I happen to know already that
the object is of a numeric type, say int, is there a way to change the
value of x in place to a different number? In the C/API documentation I
found routines to increment or decrement it in place, but I didn't find a
routine to simply replace its value by a different value. (I suppose I
could change it to the new value via an increment or decrement, but this
is ugly as well as being susceptible to overflow problems and roundoff
errors in the case of floats.)

Thanks,
Steve Vavasis

Benjamin Peterson 05-14-2009 06:47 PM

Re: C API: how to replace python number object in place?
 
Stephen Vavasis <vavasis <at> cpu111.math.uwaterloo.ca> writes:

>
> If x is a C variable of type PyObject*, and I happen to know already that
> the object is of a numeric type, say int, is there a way to change the
> value of x in place to a different number? In the C/API documentation I
> found routines to increment or decrement it in place, but I didn't find a
> routine to simply replace its value by a different value. (I suppose I
> could change it to the new value via an increment or decrement, but this
> is ugly as well as being susceptible to overflow problems and roundoff
> errors in the case of floats.)


Even in the C-API, Python ints and longs are immutable. You can convert it to a
C int and work with it, otherwise you have to use the APIs which create new
objects: PyNumber_Add etc...




Hrvoje Niksic 05-14-2009 08:59 PM

Re: C API: how to replace python number object in place?
 
vavasis@cpu111.math.uwaterloo.ca (Stephen Vavasis) writes:

> If x is a C variable of type PyObject*, and I happen to know already
> that the object is of a numeric type, say int, is there a way to
> change the value of x in place to a different number? In the C/API
> documentation I found routines to increment or decrement it in
> place, but I didn't find a routine to simply replace its value by a
> different value.


The only thing you can do is change x to point to a different object,
with the new desired value. That is how the "in place" number
modification functions work.

Which routines to increment or decrement the number are you referring
to, exactly?

Stephen Vavasis 05-15-2009 03:24 AM

Re: C API: how to replace python number object in place?
 
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. 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.

-- Steve Vavasis


In article <JrudnVSRisF_5pHXnZ2dnUVZ_rOdnZ2d@pdx.net>,
Scott David Daniels <Scott.Daniels@Acm.Org> wrote:

>If you do figurte out how to do what you want, you will put us in the
>old FORTRAN trap: People can write code that changes the value of a
>constant.
>The code:
> month = 12 # make it december
> inches_per_foot = 12
> make_previous(month)
> print ('Month is now: %s, inches_per_foot = %s' % (
> month, inches_per_foot))
>might print:
> Month is now: 11, inches_per_foot = 11
>
>--Scott David Daniels
>Scott.Daniels@Acm.Org




Carl Banks 05-15-2009 04:14 AM

Re: C API: how to replace python number object in place?
 
On May 14, 8:24*pm, vava...@cpu111.math.uwaterloo.ca (Stephen Vavasis)
wrote:
> 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


All times are GMT. The time now is 07:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.