0

I am coming from a C++ programming background and am wondering if there is a pass by reference equivalent in python. The reason I am asking is that I am passing very large arrays into different functions and want to know how to do it in a way that does not waste time or memory by having copy the array to a new temporary variable each time I pass it. It would also be nice if, like in C++, changes I make to the array would persist outside of the function.

Thanks in advance, Jared

Jared
  • 77
  • 6

1 Answers1

4

Python handles function arguments in the same manner as most common languages: Java, JavaScript, C (pointers), C++ (pointers, references).

All objects are allocated on the heap. Variables are always a reference/pointer to the object. The value, which is the pointer, is copied. The object remains on the heap and is not copied.

dsh
  • 12,037
  • 3
  • 33
  • 51