Search code examples
pythonvariablespass-by-reference

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?


Solution

  • 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.