Random832 <random...@fastmail.com> writes:

> Akira Li <4kir4...@gmail.com> writes:
>>Rustom Mody <rustompm...@gmail.com> writes:
>>> viz. I have two variables (or names!) say a and b which look the same
>>>>>> 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
>>
>> 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[1] than it works in this case as well as any other (take *a*,
*b* nametags, put them on the corresponding balloons that represents
list objects).

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
> works.
> (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[0], lst[0]]
  b = [lst[1], lst[2]]

Note: there could be no a[0][0], a[0][1], 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:

  class U:
     def __len__(self):
         return sys.maxunicode + 1
     def __getitem__(self, index):
         if index < 0:
             index += len(self)
         if 0 <= index < len(self):
             return chr(index)
         raise IndexError(index)

  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
*collections.abc.Sequence* compatible).

Or this:

  lst = [range(1, 3) for _ in range(3)]
  a = [lst[0], lst[0]]
  b = [lst[1], lst[2]]

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
Python 3.

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.


[1]
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#python-has-names
[2]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0A&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=6
[3]
http://www.pythontutor.com/visualize.html#code=a0+%3D+a1+%3D+%5B1,+2%5D%0Ab0+%3D+%5B1,+2%5D%0Ab1+%3D+%5B1,+2%5D%0Aa+%3D+%5Ba0,+a1%5D%0Ab+%3D+%5Bb0,+b1%5D%0Adel+a0,+a1,+b0,+b1%0Aa%5B0%5D%5B0%5D+%3D+%22Oops!%22&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=3&rawInputLstJSON=%5B%5D&curInstr=7


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to