Dear Friends,
Python assignment is a reference assignment. However, I really can't
explain the difference in the following example. When the object is
a list, the assignment seems to be a reference. However, when the
object is a number, the story is so different. I don't understantd
for what reason Python deals with them differently. The example is
as follows:
[color=blue][color=green][color=darkred]
>>> a = [3, 2,1]
>>> b = a
>>> a,b[/color][/color][/color]
([3, 2, 1], [3, 2, 1])[color=blue][color=green][color=darkred]
>>> a[1]=0
>>> a,b[/color][/color][/color]
([3, 0, 1], [3, 0, 1])[color=blue][color=green][color=darkred]
>>> c = 1
>>> d = c
>>> c,d[/color][/color][/color]
(1, 1)[color=blue][color=green][color=darkred]
>>> c=0
>>> c,d[/color][/color][/color]
(0, 1)
Thanks a lot.
Best,
Haoyu
Python assignment is a reference assignment. However, I really can't
explain the difference in the following example. When the object is
a list, the assignment seems to be a reference. However, when the
object is a number, the story is so different. I don't understantd
for what reason Python deals with them differently. The example is
as follows:
[color=blue][color=green][color=darkred]
>>> a = [3, 2,1]
>>> b = a
>>> a,b[/color][/color][/color]
([3, 2, 1], [3, 2, 1])[color=blue][color=green][color=darkred]
>>> a[1]=0
>>> a,b[/color][/color][/color]
([3, 0, 1], [3, 0, 1])[color=blue][color=green][color=darkred]
>>> c = 1
>>> d = c
>>> c,d[/color][/color][/color]
(1, 1)[color=blue][color=green][color=darkred]
>>> c=0
>>> c,d[/color][/color][/color]
(0, 1)
Thanks a lot.
Best,
Haoyu
Comment