0

I am using a C++ SDK where there is a function like (it has a python wrapper, but not docs):

getPos ( int uvId, float & u, float & v ) const 

How do I specify in Python so that the passed variables are changed?

I tried this example to see if I could modify floats inside a function, but it didn't work, so printed 12.0:

def change ( a ) :

    a = 35.0


b = 12.0

change ( b )

print b

So how do I call this function that I can change 2 external floats in Python?

Related:

Community
  • 1
  • 1
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • I've added link to related question – jfs Feb 23 '09 at 23:16
  • See how the arg for `Operation()` function is handled: http://stackoverflow.com/questions/540427/c-to-python-via-swig-cant-get-void-parameters-to-hold-their-value – jfs Feb 23 '09 at 23:25
  • @Joan Venge: "Basic types" doesn't have any meaning in Python. I think you're talking about "immutable" types. Or you might be talking about C-language types. You might want to update your question. – S.Lott Feb 24 '09 at 00:24
  • Thanks, I thought they were called basic types in python too. So all strings, ints, floats are immutable? – Joan Venge Feb 24 '09 at 00:33
  • As are tuples. Probably a few other things. But the things which are "primitive" in Java and C++ are (generally) immutable objects in Python so they're similar to other languages. – S.Lott Feb 24 '09 at 00:35

4 Answers4

5

In Python:

def getPos(uvID):
    # compute u, v
    return u, v

# 
u, v = getPos(uvID)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

As far I know, Python doesn't support call-by-reference, so the exact code you are suggesting doesn't work (obviously).

The tool (or person) that generated the Python wrapper for the C++ function must have done something special to support this function (hopefully, or you won't be able to use it). Do you know what tool was used to generate the wrapper?

Usually tools like this will generate some sort of container data type:

b.value = 12.0
change(b)
print b.value
Tom Lokhorst
  • 13,658
  • 5
  • 55
  • 71
  • Thanks, they used Swig, but I don't know how they could do it without changing the return type. – Joan Venge Feb 23 '09 at 23:22
  • To be fair, everything is (kinda) call-by-reference, just that numbers are immutable. So you are not changing attributes of the object bound to the name a in the function, but rebinding a to a completely different object. – Mike Boers Feb 24 '09 at 16:50
  • Thanks Mike, this is what I was thinking. – Joan Venge Feb 25 '09 at 19:16
  • I'm not an expert in Python, but I think everything is passed by value (by copying). It's just that since everything is an object, the thing that gets copied is always a reference (yeah, that sounds confusing). – Tom Lokhorst Feb 25 '09 at 19:55
  • So, you always have copies of a reference to some object. However, some objects (like lists) you can change, while others (like numbers or tuples) can't be changed, they're immutable. – Tom Lokhorst Feb 25 '09 at 19:56
2

You're going to have to resort to ctypes.

Specifically, see https://docs.python.org/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

For simple cases have the function return the new value.

For more complicated cases you can pass in a object or a list and have that changed:

def foobar(alist):
    alist[0] = 10

blist = [42]
foobar(blist)
print blist[0]

Edit:

For wrapping C++ references there isn't any standard way (basic python interfaces are at the C level - not C++) - so it depends how the python interface has been implemented - it might be arrays, or returning multiple values. I'm not sure how boost.python handles it but you might start there, or maybe look under a debugger to see how the parameter are handled.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
  • Thanks but the getPos function and many more is C++ and I can't change them. Everything is wrapped in Python so I assume there must be a way to use it. – Joan Venge Feb 23 '09 at 23:06