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

Discussion in 'Python' started by Stephen Vavasis, May 14, 2009.

  1. 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
     
    Stephen Vavasis, May 14, 2009
    #1
    1. Advertising

  2. 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...
     
    Benjamin Peterson, May 14, 2009
    #2
    1. Advertising

  3. (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?
     
    Hrvoje Niksic, May 14, 2009
    #3
  4. 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 <>,
    Scott David Daniels <> 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
    >
     
    Stephen Vavasis, May 15, 2009
    #4
  5. Stephen Vavasis

    Carl Banks Guest

    On May 14, 8:24 pm, (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
     
    Carl Banks, May 15, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. martin
    Replies:
    1
    Views:
    492
    Rick Strahl [MVP]
    Jul 19, 2004
  2. Replies:
    9
    Views:
    592
    justanotherguy
    Dec 3, 2004
  3. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    396
    Praveen, Tayal (IE10)
    Mar 17, 2005
  4. Eric Boucher
    Replies:
    31
    Views:
    612
    Ross Bamford
    Mar 29, 2006
  5. Giacomo Alzetta
    Replies:
    11
    Views:
    283
    Stefan Behnel
    Aug 11, 2012
Loading...

Share This Page