Question

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:

Was it helpful?

Solution

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

OTHER TIPS

In Python:

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

# 
u, v = getPos(uvID)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top