Terminology: "reference" versus "pointer"

Jussi Piitulainen harvesting at makes.email.invalid
Sat Sep 12 22:02:51 CEST 2015

Rustom Mody writes:

>>>> a
> [[1,2],[1,2]]
>>>> b
> [[1,2],[1,2]]
> And yet doing
>>>> a[0][0] = "Oops!"
> gives a data structure one "Oops!"
> whereas doing it to b mysteriously gives 2
> Best I can see you can only explain this 
> seemingly-similar-but-structurally-different
> with box-n-arrow diagrams.
> Or some moral equivalent.

I think the best way is to say that a[0] and a[1] are the same object,
while b[0] and b[1] are different objects. Possibly b[0] is also the
same object as a[0] and a[1]. Then b[0][0] = "Oops!" was redundant.

And the way to work out whether two objects are the same is to trace
where they come from. Certain pieces of code result in new objects.
Other pieces of code pass old objects around. Possibly store them in
places, or change them in some way. Never make copies.

Works for me.

Some sort of pointer-talk is useful to discuss the implementation of all
this (how can an object be in different places? how does an arbitrary
object fit in a 64-bit register?) but for ordinary reasoning about a
program, pointer-talk with those diagrams mostly gets in the way.

More information about the Python-list mailing list