what happens inside?

Andrew Berg bahamutzero8825 at gmail.com
Wed Jun 22 11:53:03 EDT 2011


On 2011.06.22 10:45 AM, Chetan Harjani wrote:
> why tuples are immutable whereas list are mutable?
Tuples are more efficient and more appropriate for a list of items that
doesn't need to change.
> why when we do x=y where y is a list and then change a element in x, y
> changes too( but the same is not the case when we change the whole
> value in x ), whereas, in tuples when we change x, y is not affected
> and also we cant change each individual element in tuple. Someone
> please clarify.
With x = y, x and y point to the same place in memory. Because lists are
mutable, that place in memory can change, so both x and y will point to
the same changed spot in memory. Because tuples are immutable, that
place in memory cannot change, so Python must make a new spot in memory
for the changed object. This is true for all immutable objects.



More information about the Python-list mailing list