Terminology: "reference" versus "pointer"
4kir4.1i at gmail.com
Sun Sep 13 20:04:20 CEST 2015
Random832 <random832 at fastmail.com> writes:
> Akira Li <4kir4.1i at gmail.com> writes:
>>Rustom Mody <rustompmody at gmail.com> writes:
>>> viz. I have two variables (or names!) say a and b which look the same
>>> And yet doing
>>>>>> a = "Oops!"
>>> gives a data structure one "Oops!"
>>> whereas doing it to b mysteriously gives 2
>> Sorry, I haven't followed the whole thread. Could your provide a
>> complete code example? Mention what you expect to happen and what
>> happens instead in your case.
> a0 = a1 = [1, 2]
> b0 = [1, 2]
> b1 = [1, 2]
> a = [a0, a1]
> b = [b0, b1]
> del a0, a1, b0, b1
> There's nothing about *him* expecting anything wrong to happen. The
> question is how to draw a diagram that unambiguously shows the resulting
> structure using the "parcel tags" model shown in the diagrams (and
> without having a0/a1/etc as actual names)
I'm not sure what "parcel tags" model is but if you mean these
pictures than it works in this case as well as any other (take *a*,
*b* nametags, put them on the corresponding balloons that represents
The only names left are *a* and *b* that refer to the corresponding
lists. There is no ambiguity there to put *a*, *b* nametags.
Lists as any other containers contain references to other objects and
therefore "box and arrows" model provides _more details_ here[2,3]
> If the "parcel tags" model can't show it, then the "parcel tag" model
> clearly is not a correct and complete depiction of how Python actually
> (If I were drawing a picture rather than ASCII I'd add something to make
> it clear that the pairs shown are list objects Like, it's a circle with
> the word "list" and two pointer-boxes inside it.)
"correct and complete" is impossible in the general case for any model.
Imagine what happens if numpy arrays are used instead of Python lists:
lst = [numpy.array([1, 2]) for _ in range(3)]
a = [lst, lst]
b = [lst, lst]
Note: there could be no a, a, etc corresponding Python
objects until you explicitly reference them in the code. If there are no
Python objects then you can't draw any arrows and therefore "box and
arrows" model is incomplete.
Or consider this collections of all Unicode characters:
return sys.maxunicode + 1
def __getitem__(self, index):
if index < 0:
index += len(self)
if 0 <= index < len(self):
u = U()
print(u[0x61]) # -> a
In this example, it is even more clear that the corresponding items
might not exist until they are explicitly referenced in a program. *u*
is a sequence as any other in Python (it is easy to make it
lst = [range(1, 3) for _ in range(3)]
a = [lst, lst]
b = [lst, lst]
In Python 2, it is the exact replica of the original example. In Python
3, it is the same if you don't need to mutate the ranges. Again, the
leaf objects might not exist until you reference them in the code in
Finally, consider a Python sequence that uses os.urandom()
internally. No model will predict the result (and therefore no model is
complete) in this case.
More information about the Python-list