Is there any way (hack) to push Python function (def) to return results by reference even for immutable types?
A proposal application (swap as subroutine):
def swap(a, b):
.....a,b = b,a
Note:
def swap(a, b):
.....return b,a
works as function which is not the answer of the question!
For example there is a function random.shuffle(a) that works in-place.
My idea is to call a function written in Fortran/C++ and call them via Python. It does work but has disadvantages too.
note:
Both “lambda” and “def” (as function) have the following problem: a, b = swap(a, b) which requires care about order of variables. In my proposal (if it was possible) the subroutine is used as: swap(a, b) so there is no requirement to care about order of variable.
All names in Python are references. And no, there are no “out” references (e.g. in a C++ sense) available. You need to pass a mutable object and then you can mutate it in the function. But then again, returning new value(s) should be the preferred way.