Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass values by ref in Python?

Basically I am using the C++ API of an app, but there is no reference for its python access. One variable is passed by ref, like so:

GetPoint ( Point  &p, Object obj )

so how can I translate to Python? Is there a pass by ref symbol?

like image 879
Joan Venge Avatar asked Sep 13 '26 02:09

Joan Venge


1 Answers

There is no pass by reference symbol in Python.

Just modify the passed in point, your modifications will be visible from the calling function.

>>> def change(obj):
...     obj.x = 10
...
>>> class Point(object): x,y = 0,0
...
>>> p = Point()
>>> p.x
0
>>> change(p)
>>> p.x
10

...

So I should pass it like: GetPoint (p, obj)?

Yes, though Iraimbilanja has a good point. The bindings may have changed the call to return the point rather than use an out parameter.

like image 82
Aaron Maenpaa Avatar answered Sep 14 '26 15:09

Aaron Maenpaa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!