You will commonly hear that Python has names, not "variables" in the traditional sense. It also doesn't have "pointers" since such a concept is only meaningful when working at a low level, like with C. (For this reason, you can get "pointers" if you use a C compatibility library in Python, like ctypes
. But, such pointers are meant only for interoperability with C.)
Most of the functionality you would use a pointer for in C is present in some other form in Python. For example:
- Returning multiple values from a function. In C, you'd do
int foo(int *, int *)
to return two ints (and maybe an error code). In Python, just use tuple unpacking: x, y = foo()
with return x, y
in foo
.
- Passing in big objects. Since everything is pass-by-reference anyway, you can safely pass a reference to a huge object without having it get copied.
Modifying input parameters. Most of this is possible by choosing an appropriate mutable input. For example, you can pass a list into a function and modify the list within the function. In Python, since the references are the same, the input list will appear to be modified:
def change_list(l):
l[0] = 3
my_list = [1,2,3]
change_list(my_list)
print(my_list) # prints [3, 2, 3]
MATLAB has a different concept of variable than Python does. In MATLAB, a variable is a matrix with some dimensions. Writing x = y
effectively copies y
into x
(setting x
s dimensions appropriately). MATLAB internally does optimizations to avoid actually copying data unless necessary, but for the programmer it will seem as if x
and y
are separate matrices.
MATLAB also doesn't have pointers unless you are dealing with a compiled MEX extension (which is written in some other language like C). Like Python, you have mechanisms for passing things around without needing pointers (multiple input and output arguments, for example).