Terminology: “reference” versus “pointer”
Steven D'Aprano
steve at pearwood.info
Sat Sep 12 12:32:22 EDT 2015
On Sat, 12 Sep 2015 02:42 pm, Random832 wrote:
> Anyway, maybe we do need a term to distinguish Python/C#/Java pointers
> from C/C++ pointers - maybe call it a "non-arithmetic" pointer, since
> the key thing about it is you can't do pointer arithmetic on them to get
> the object "next to" the one it points at.
How about *just don't call them pointers*? You know, since they aren't
pointers in the computer science sense.
The Free On-line Dictionary of Computing defines "pointer":
1. <programming> An address, from the point of view of a
programming language. A pointer may be typed, with its type
indicating the type of data to which it points.
Arithmetic is irrelevant to the definition of "pointer". That's a foible of
C, not fundamental to the concept of pointer. Pascal, for example, has
pointers too, implemented pretty much in the same way as C/C++, but the
type system doesn't allow you to perform arithmetic on them because they
aren't treated as integers.
Computer science and IT is *dominated* by a single usage for "pointer" --
it's an abstract memory address. The fundamental characteristics of
pointers are:
- they are first-class values; you can assign a pointer to a variable;
- you can dereference a pointer: get the value pointed to;
- (in some languages) you can get a pointer to a specific variable (possibly
an unnamed, dynamic variable allocated in the heap rather than a named,
statically allocated variable).
The last two imply that the language must be one where values have fixed
addresses, not just as a matter of implementation, but as a matter of
language semantics. If they are free to move, they cannot have a fixed
address.
Python, Java, Ruby, Lua, Javascript etc. have *no such concept* as pointer.
There are no values in these languages which correspond to the standard
definition of pointer:
- you cannot get a pointer to an object or a variable (a name);
- since there are no pointers, you cannot dereference them;
- or assign them to a variable.
Since pointers can be considered an indirect reference to a value, what sort
of indirect references does Python have? The simplest thing in Python that
is somewhat analogous to a pointer is a *name*, since names allow you to
indirectly refer to some value:
x = 23
print(x) # prints the value referred to by x, not "x" itself.
ptr = "x" # equivalent to "the address of x"
y = globals()[ptr] # equivalent to dereferencing
Note that names are not first-class values in Python: there is no Name type,
and you cannot bind a name to a variable, you have to use a string.
It's not a very close analogy, but it's the closest Python has.
Implementations of Python, Javascript, Lua, Java, Ruby etc. *may or may not*
use pointers in the implementation, but they are not part of the language
interface. These languages have no "addressof" operator, no dereference
operator, and no "pointer" type; values do not necessarily have fixed
addresses (indeed, in Jython and IronPython, values can move in memory).
Insisting that Python has pointers is like insisting that you use a text
editor by flipping bits. "What happens if I press Ctrl-X?" "Well, these
bits on the screen flip from black to white, these bits flip from white to
black, and these stay the same."
--
Steven
More information about the Python-list
mailing list