3

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?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689
  • Somewhat related question with some relevant answers: http://stackoverflow.com/questions/534375/passing-values-in-python – Dana Feb 23 '09 at 18:03
  • Thanks forgot about that one. – Joan Venge Feb 23 '09 at 18:08
  • 1
    I know this is an old question, but for future readers: When using Python with other language's APIs, you should be able to access members directly, rather than using the getters and setters. If the Python bindings are any good, then the attributes will actually be [properties](http://docs.python.org/library/functions.html#property), and you'll be redirected through the accessors without knowing it. Getters and setters are not very pythonic, so I've made a point of not using them when using wxPython, or Swing from Jython. – Cam Jackson Feb 02 '12 at 00:29

5 Answers5

5

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.

Community
  • 1
  • 1
Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
  • python has global variables that are visible to the class and other functions. it has variables internal to the class that are initialized by the constructure _self method. python has local variables. python has private variables in subclassed methods. use copy to create a new instance of data from one variable to another. – Golden Lion May 26 '21 at 22:09
3

It's likely that your Python bindings have turned that signature into:

Point GetPoint(Object obj)

or even:

Point Object::GetPoint()

So look into the bindings' documentation or sources.

  • Thanks, the return is a bool for status, but the point must be passed, otherwise, it throws wrong number of args exception. – Joan Venge Feb 23 '09 at 18:07
2

I'm pretty sure Python passes the value of the reference to a variable. This article can probably explain it better than I.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
1

Objects are always passed as reference in Python. So wrapping up in object produces similar effect.

Xolve
  • 22,298
  • 21
  • 77
  • 125
  • This is the correct answer. Python objects are always passed in the same way (by reference). Some objects (e.g. ints) are immutable and give the impression of being passed by value... nope, it's ways the same way.... always! :-) – Salim Fadhley Feb 23 '09 at 19:27
1

There is not ref by symbol in python - the right thing to do depends on your API. The question to ask yourself is who owns the object passed from C++ to python. Sometimes, the easiest ting it just to copy the object into a python object, but that may not always be the best thing to do.

You may be interested in boost.python http://www.boost.org/doc/libs/1_38_0/libs/python/doc/index.html

David Cournapeau
  • 78,318
  • 8
  • 63
  • 70