0

I'm a physics student, mostly programming in Python and Matlab, now getting into C to boost the performance of my simulations.

Simple question: are there pointers in Python? Are there pointers in Matlab? If yes, can I use them with similar "ease" as in C?

I know that behind the high-levelness in Python and Matlab, there are lots of pointers at work. The question is: can I do something like this C code explicitly in Python or Matlab:

int *p;
int someValue = 1;
p = &someValue;
doSomethingForMe = callSomeFunction(p);

I know there's probably no point in doing that anyway (for most applications). I just want to learn.

seb
  • 2,251
  • 9
  • 30
  • 44
  • 4
    This is a classic example of an A/B question. You're asking about pointers, but actually you don't care about pointers at all, you care about the sort of things that C requires pointers for. As nneonneo points out, for most of those cases you don't require pointers in Python. – Daniel Roseman Mar 08 '13 at 09:08
  • 1
    this is a quite sensible differentiation to make though, when you are just learning those concepts (as I do). usually you just ask yourself "what the hell? why can't I do that in Python?" – seb Mar 08 '13 at 09:15

2 Answers2

7

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 xs 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).

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Really helpful answer! thank you! I am playing around with SciPy's weave-in functionality of C code. It delivers good results, but is only useful for smaller scale simulation codes. I'm thinking about getting into python's C-API. From what you write, I get that you can only port so much functionality from C to Python (or Matlab). – seb Mar 08 '13 at 09:06
  • Depends. The fact that Python and MATLAB are high-level languages means you can command a lot more power through fewer lines. Good MATLAB code, especially, is likely to run *much* faster than anything you could write in straight C (i.e. no libraries), simply because it is easy to write powerful code, and the MATLAB engine will automatically take advantage of the CPU's capabilities (multicore, vector instructions, etc). – nneonneo Mar 08 '13 at 09:15
  • ok. I shall play around a bit more then. – seb Mar 08 '13 at 09:21
0

Python you can have pointers with ctypes library apparently. Also, you can simulate the notion of pointers in Python, like in this question.

In Matlab you don't have pointers either.

Community
  • 1
  • 1
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415