passing PyIntType objects by reference

Discussion in 'Python' started by Patrick Stinson, Jun 10, 2004.

  1. when passing an python integer value to a C function
    ie.
    x = 1
    mymod.myfunc(x)

    is it possible to change the value of the python object "x" as happens in c
    when you pass a pointer to an int? Is there something fundamentally wrong
    with this idea, as this does not happen in pure python anyway?

    Better yet, I'm wrapping a C api where some of the values are returned by
    passing values by reference. Is the only way to simulate this to return a
    list with the returned values?

    Cheers
     
    Patrick Stinson, Jun 10, 2004
    #1
    1. Advertising

  2. Patrick Stinson

    Larry Bates Guest

    Why not do following:

    result=mymod.myfunc(x)

    That way you get a proper return value and I also think
    it is clearer what you are doing.

    You can pass a memory buffer (see dynawin's membuf)

    from dynwin.windll import membuf
    import struct

    t=membuf(x)
    mymod.myfunct(t.address())
    x=struct.unpack('l',t.read())

    I'm assuming x is a long (interger)

    Normally I combine the two methods and return the value
    passed back as a normal Python return value.

    HTH,
    Larry Bates
    Syscon, Inc.


    "Patrick Stinson" <> wrote in message
    news:...
    > when passing an python integer value to a C function
    > ie.
    > x = 1
    > mymod.myfunc(x)
    >
    > is it possible to change the value of the python object "x" as happens in

    c
    > when you pass a pointer to an int? Is there something fundamentally wrong
    > with this idea, as this does not happen in pure python anyway?
    >
    > Better yet, I'm wrapping a C api where some of the values are returned by
    > passing values by reference. Is the only way to simulate this to return a
    > list with the returned values?
    >
    > Cheers
     
    Larry Bates, Jun 10, 2004
    #2
    1. Advertising

  3. Patrick Stinson

    Peter Otten Guest

    Patrick Stinson wrote:

    > when passing an python integer value to a C function
    > ie.
    > x = 1
    > mymod.myfunc(x)
    >
    > is it possible to change the value of the python object "x" as happens in
    > c when you pass a pointer to an int? Is there something fundamentally
    > wrong with this idea, as this does not happen in pure python anyway?


    >>> a = 1
    >>> b = 1
    >>> a is b

    True

    If you somehow manage to change a's value into 42, you will change every 1
    to 42.

    Peter
     
    Peter Otten, Jun 10, 2004
    #3
    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. sam pal
    Replies:
    3
    Views:
    594
    E. Robert Tisdale
    Jul 16, 2003
  2. Raga
    Replies:
    0
    Views:
    324
  3. hardwareman
    Replies:
    4
    Views:
    376
    Peter Julian
    Jun 28, 2005
  4. ghoetker
    Replies:
    1
    Views:
    395
  5. Adam Akhtar
    Replies:
    11
    Views:
    184
    Xavier Noria
    Aug 28, 2008
Loading...

Share This Page