confused about Python assignment
Emile van Sebille
emile at fenx.com
Thu Oct 30 21:07:09 EST 2003
"Haoyu Zhang" <flyfeather at myrealbox.com> wrote in message
news:d5a952e4.0310301749.3126b961 at posting.google.com...
> 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:
>
> >>> a = [3, 2,1]
bind the label 'a' to the mutable list [3,2,1]
> >>> b = a
bind 'b' to what 'a' is bound to
> >>> a,b
> ([3, 2, 1], [3, 2, 1])
> >>> a[1]=0
replace index 1 in what 'a' is bound to with '0'
> >>> a,b
> ([3, 0, 1], [3, 0, 1])
> >>> c = 1
bind c to the immutable integer 1
> >>> d = c
bind 'd' to what 'c' is bound to
> >>> c,d
> (1, 1)
> >>> c=0
now bind c to the immutable integer 0
> >>> c,d
> (0, 1)
HTH,
Emile van Sebille
emile at fenx.com
More information about the Python-list
mailing list