[Tutor] When is = a copy and when is it an alias
spir
denis.spir at gmail.com
Tue Jan 28 09:28:29 CET 2014
On 01/27/2014 06:04 PM, Denis Heidtmann wrote:
> Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this
> case. a[0] is not a reference to b[0]. I think I see the essential
> distinction. Experience will complete the picture for me.
"symolic assignment" is my term, so whatever I mean with it qualifies as
symbolic assignment ;-); and tes, your example a[0]=b[0] is indeed a symbolic
assignment (because the right side "b[0]" denotes a value, an object, and could
be on the left side of an assignment)
the distinction between "replacement" & "modification" also uses my terms;
better you know that because if you talk with programmers using these terms as
key words, others will be surprised
The real point is: maybe read again my previous post. I insisted on the fact
that in python _symbols_ are not corelated, never ever. Your remark here seems
to show that you expect a[0] and b[0] to be corelated, in such a way that if we
change one of them the second should follow. No. Values are made unique by
symbolic assignment; but this can only show if you modify them partly (not
replace globally), thus can only show if those are complex values.
>>> a = [1, [1,2]]
>>> b = a
>>> b
[1, [1, 2]]
>>> b is a
True
a's and b's values are a single, unique object... as long as I only modifie them
(the values) partly:
>>> a = [1,[2,3]]
>>> a[0] = 0
>>> b
[0, [1, 2]]
>>> a[1] = [0,0]
>>> b
[0, [0, 0]]
>>> a is b
True
>>> a[0] = b[0]
>>> a[0] is b[0]
True
Here i make their first elements the same unique value. But I'm blocked to show
it (other than using 'is') because I cannot modify such objects (they are simple
numbers). Since it is the _values_ which are related, not the symbols, if I
replace one of them I break the relation.
>>> a[0] = 9
>>> a
[9, [2, 3]]
>>> b
[1, [0, 0]]
Right? On the other, if I create a relatoin between their second elements, then
I can have something more interesting:
>>> a[1] = b[1]
>>> a[1] is b[1]
True
>>> a, b
([9, [0, 0]], [1, [0, 0]])
>>> a[1][1] = 9
>>> b[1][1]
9
>>> a, b
([9, [0, 9]], [1, [0, 9]])
You will get used to it... Also, it just works most of the time, except for
corner cases where you will be trapped 2-3 times until you get it. (The reason
is that unconsciously, when we want several symbols to have their proper values,
we just do it right by assigning them apart, even if they (initially) have the
same values. When instead we want a symbol to refer to the same value as
another, we correctly use a symbolic assignment; instead of incorrectly
assigning an equal, but distinct, value.]
denis
More information about the Tutor
mailing list