Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing PyIntType objects by reference

Reply
Thread Tools

passing PyIntType objects by reference

 
 
Patrick Stinson
Guest
Posts: n/a
 
      06-10-2004
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
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      06-10-2004
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" <ajole-> 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



 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      06-10-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data between objects and calling all objects of a class in turn ghoetker Python 1 08-25-2010 03:18 AM
how to avoid passing by reference and how to copy objects Adam Akhtar Ruby 11 08-28-2008 11:46 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Passing objects by reference using CORBA Raga Java 0 02-21-2007 12:30 PM
Touble with passing a reference to vector of vectors of objects hardwareman C++ 4 06-28-2005 02:40 PM



Advertisments