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

Benjamin Peterson benjamin at python.org
Thu May 14 14:47:40 EDT 2009


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...






More information about the Python-list mailing list