Sudip Bhattacharya wrote:
s=(1,2,3)
s=s+(4,5,6)
s
(1,2,3,4,5,6)

The tuple has changed.

No it hasn't. You have created a *new* tuple, and assigned it to the same name. Consider:

py> s = (1, 2, 3)
py> id(s)
3083421332
py> t = s
py> id(t)
3083421332


This shows that both s and t are names for the same tuple, with ID 3083421332. Now watch when we use += on s:

py> s += (4, 5)
py> id(s)
3083534812
py> id(t)
3083421332
py> t
(1, 2, 3)

The ID of s has changed, but t stays the same. So the original tuple remains untouched, and a new tuple is created.

If you do the same thing with lists, you will see that because lists are mutable, it does *not* create a new list, but changes the original in place:

py> a = [1, 2, 3]
py> b = a
py> a += [4, 5]
py> b
[1, 2, 3, 4, 5]


--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to