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

Scott David Daniels Scott.Daniels at Acm.Org
Thu May 14 16:20:50 EDT 2009


Stephen Vavasis wrote:
> 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
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 at Acm.Org



More information about the Python-list mailing list