[Edu-sig] How does Python do Pointers?
Michael H. Goldwasser
goldwamh at slu.edu
Sun May 4 22:57:46 CEST 2008
For students familiar with C, the most straightforward analogy is
that ALL of Python's names are akin to C's pointers. This is
certainly the closest match of the three C/C++ models (value,
Even for new programmers with no previous knowledge, we make sure to
portray names distinctly as pointers to the underlying object in
memory, rather than to cloud the role of the name versus the value.
Advantages of this analogy are the consistency of the following
* Assignment "a = b" creates an alias, whereby a and b both
reference the same underlying object.
* Parameter passing is equivalent to passing a pointer in C.
With a signature, def foo(bar):, the calling syntax
foo(myList) simply assigns the formal parameter bar to point to
the same underlying object as the actual parameter myList. In
fact, it is exactly the semantics of the standard assignment
"bar = myList" other than the distinction in scope between the
local variable and the caller's.
* None in Python is analogous to NULL in C.
As an aside, this is why I prefer to compare Python's model to
pointers in C/C++ rather than to references. When passing a
reference variable in C++, the formal parameter must be bound to
an actual object (you cannot pass a null reference).
Having used this analogy, there are a few things that should be
explained to those used to C.
* You cannot directly manipulate memory addresses in Python as you
can in C. Internally, Python's interpretter maintains those
memory address. In fact, the value of id(x) is typically the
underlying memory address. But you cannot use that address to
access anything, nor can you do pointer arithmetic as in C.
* There is an independent issue in that Python's primitive types
(int/str/tuple) are immutable. So when passing an integer as a
parameter, the mechanism is still pass-by-reference however this
does not give the callee any power to alter the original value.
I think this is why there are some who errantly characterize
Python as using pass-by-value .
As a final aside, if student have any familiarity with Java, the
Python model is quite simple to explain. It is precisely that which
Java uses for all object types.
On Sunday May 4, 2008, David MacQuigg wrote:
> This was the question from a student at my recent lecture to a class of engineering students studying C. My answer was brief: It doesn't - arguments are passed by value. Then I thought, this is really misleading for students of C, where pass-by-value means making a copy of the passed object. So I'm looking for a simple answer that is complete, but doesn't baffle freshmen whose only experience so far is an introductory course in C.
> How about this:
> Python doesn't use pointers, but they really aren't needed in Python, because the way objects in memory are accessed is fundamentally different than C. Objects in Python are "bound" to variable names. These names are associated with pointers, but that is at a lower level not seen by the Python programmer. If you need to get down to that level, you should be using C, not Python.
> ~ '''
> That's a little better than my first answer, but I could elaborate with further discussion about argument passing.
> You know that in C there are two ways to pass a value to a function: call-by-value and call-by-reference. Call-by-value puts a copy of the value in the function's memory area. Call-by-reference passes to the function a pointer to the original value in the caller's memory. In Python, passing a value to a function is done by "binding" the name of the parameter in the function to the value in the caller's memory. This is like call-by-reference in C, but the Python programmer never sees the pointer.
> ~ '''
> Note that Martelli says "all argument passing in Python is by value" (Python in a Nutshell, p.74), but I think this may be an error.
> I would be interested in seeing how others would handle this question.
> -- Dave
> P.S. The lecture went very well. The students were enthusiastic, and the Mandelbrot images were stunning! http://ece.arizona.edu/~edatools/ece175/Lecture The purpose of the lecture was to introduce students to higher-level programming, and show how C can still play a role when performance is critical. I'm hoping to expand this to a 3-week sequence of lectures.
| Michael Goldwasser
| Associate Professor
| Dept. Mathematics and Computer Science
| Saint Louis University
| 220 North Grand Blvd.
| St. Louis, MO 63103-2007
More information about the Edu-sig