TLDR: Python names work like pointers with automatic de/referencing but do not allow explicit pointer operations. Other targets represent indirections, which behave similar to pointers.
The Python language spec does not define what names and such actually are, only how they behave. However, the behaviour can be explained with pointers.
The CPython implementation uses pointers of type PyObject*
under the hood. As such, it is possible to translate name semantics to pointer operations. The key is to separate names from actual objects.
The example Python code includes both names (i
) and objects (5
).
i = 5 # name `i` refers to object `5`
j = i # ???
j = 3 # name `j` refers to object `3`
This can be roughly translated to C code with separate names and objects.
int three=3, five=5; // objects
int *i, *j; // names
i = &five; // name `i` refers to position of object `5`
j = i; // name `j` refers to referent of `i`
j = &three; // name `j` refers to position of object `3`
The important part is that "names-as-pointers" do not store objects! We did not define *i = five
, but i = &five
. The names and objects exist independent from each other.
Names only point to existing objects in memory.
When assigning from name to name, no objects are exchanged! When we define j = i
, this is equivalent to j = &five
. Neither i
nor j
are connected to the other.
+- name i -+ -\
\
--> + <five> -+
/ | 5 |
+- name j -+ -/ +----------+
As a result, changing the target of one name does not affect the other. It only updates what that specific name points to.
Python also has other kinds of name-like elements: attribute references (i.j
), subscriptions (i[j]
) and slicing (i[:j]
). Unlike names, which refer directly to objects, all three indirectly refer to elements of objects.
The example code includes both names (i
) and a subscription (i[0]
).
i = [1,2,3] # name `i` refers to object `[1, 2, 3]`
j = i # name `j` refers to referent of `i`
i[0] = 5 # ???
A CPython list
uses a C array of PyObject*
pointers under the hood. This can again be roughly translated to C code with separate names and objects.
typedef struct{
int *elements[3];
} list; // length 3 `list` type
int one = 1, two = 2, three = 3, five = 5;
list values = {&one, &two, &three}; // objects
list *i, *j; // names
i = &values; // name `i` refers to object `[1, 2, 3]`
j = i; // name `j` refers to referent of `i`
i->elements[0] = &five; // leading element of `i` refers to object `5`
The important part is that we did not change any names! We did change i->elements[0]
, the element of an object both our names point to.
Values of existing compound objects may be changed.
When changing the value of an object through a name, names are not changed. Both i
and j
still refer to the same object, whose value we can change.
+- name i -+ -\
\
--> + <values> -+
/ | elements | --> [1, 2, 3]
+- name j -+ -/ +-----------+
The intermediate object behaves similar to a pointer in that we can directly change what it points to and reference it from multiple names.
i
should've been equal to3
i = j
entails that the value named byj
is what gets assigned, not "the variablej
".